转:http://blog.csdn.net/wang4237/article/details/5306335

SharePoint2010的页面风格发生了很大的改变,其页面风格类似于Office的视图,这种视图被称为Ribbon视图。
 
其中SharePoint里面对应得控件为SPRibbon控件。在Master page里面是这么添加这个控件的:
<SharePoint:SPRibbon runat="server" PlaceholderElementId="RibbonContainer" CssFile="">.......</SharePoint:SPRibbon>
 
在使用之前必须将Ribbon的XML结构定义好,XML路径为/TEMPLATE/GLOBAL/XML/CMDUI.XML。这个XML的具体结构,如何定义还在研究当中,会在之后发布。
 
然而这些并不够,在SharePoint2010里面,不同的页面有不同ribbon,如何去为页面设置它的ribbon,并设置Ribbon中的Button的页面弹出框的URL。
 
经过查看SharePoint源代码,在页面的后台代码中发现这么一段代码:
        SPRibbon current = SPRibbon.GetCurrent(this);
if (current !=
null)

        {

            current.CommandUIVisible
= true;

            current.MakeTabAvailable("Ribbon.Read");

            current.MakeTabAvailable("Ribbon.WebApp");

            current.InitialTabId
= "Ribbon.WebApp";

            current.Minimized
= false;

            SPRibbonScriptManager manager = new
SPRibbonScriptManager();

            List<IRibbonCommand>
commands = new List<IRibbonCommand>();

            bool admin = base.GlobalAdmin.IsCurrentUserMachineAdmin();

            commands.Add(new
SPRibbonCommand("WebAppTab"));

      ......
        }
在之前给大家介绍了Ribbon的主体结构。在这次主要为大家介绍如何实现页面的定制,即在页面中如何控制ribbon中这些Button是如何Enable和Disable的和弹出页面,以及一些SharePoint的比较适用的JS方法。
 
首先现在TEMPLATE/GLOBAL/XML/CMDUI.XML中定义Tab结构。在这个XML中定义好在页面上Ribbon的Button,以及这些Button的Text,ToolTil, Image,以及排列方式。
 
之后要实现页面的Ribbon,需为这个页面定义一个JS对象,在这个对象中要声明一些方法:
仿照SharePoint,定义方式如下:
Type.registerNamespace("SP.UI.Admin");
SP.UI.Admin.TestPageComponent = function() {

    ULSITv: ;

    //SP.UI.Admin.ServiceApplicationPageComponent.initializeBase(this);

};
SP.UI.Admin.TestPageComponent.TestIsEnabled = function(a) {

    return true;

};

SP.UI.Admin.TestPageComponent.TestNavigate = function(a, b, c) {

        var b = SP.UI.$create_DialogOptions();

        b.url = a; b.dialogReturnValueCallback = SP.UI.Admin.TestPageComponent.$5_1;

        SP.UI.ModalDialog.showModalDialog(b)

};
SP.UI.Admin.TestPageComponent.prototype = {

    getFocusedCommands: function() {

        ULSITv: ; return []

    },

    getGlobalCommands: function() {

        ULSITv: ;

        return getGlobalCommands()

    },

    isFocusable: function() {

        ULSITv: ;

        return true

    },

    receiveFocus: function() {

        ULSITv: ;

        return true

    },

    yieldFocus: function() {

        ULSITv: ;

        return true

    },

    canHandleCommand: function(a) {

        ULSITv: ;

        return commandEnabled(a)

    },

    handleCommand: function(b, a, c) {

        ULSITv: ;

        return handleCommand(b, a, c)

    },

    getId: function() {

        ULSITv: ;

        return "TestPageComponent"

    },

    init: function() {

        ULSITv: ;

    },

};
prototype的getId和init方法,在SharePoint的其他JS并没有定义,但是在DEBUG代码时却发现SharePoint的JS类有这个两个方法,目前没找到他们是在哪定义,我在这直接定义,不定义就会异常。
 
SP.UI.Admin.TestPageComponent.registerClass("SP.UI.Admin.TestPageComponent", CUI.Page.PageComponent);
1.点击Ribbon的Button是触发的方法:TestNavigate
2.决定Ribbon的Button是否Enable的方法:TestIsEnabled
写完JS之后,需要在代码声明一个继承 SPRibbonCommand的类TestCommand ,
在这个类实现如下
internal sealed class TestCommand : SPRibbonCommand

    {

        public TestCommand(string commandId, string target, TestNavigateOptions options, bool admin)

            : base(commandId, null, null)

        {            

            base.HandlerStatement = string.Format(CultureInfo.InvariantCulture, "SP.UI.Admin.TestPageComponent.TestNavigate('{0}', {1}, {2})", new object[] { SPHttpUtility.EcmaScriptStringLiteralEncode(target), "true", "true");

            base.EnabledStatement = "SP.UI.Admin.TestPageComponent.TestIsEnabled(0)";

        }

}
 
 
然后在页面的后台类中写入如下代码:
protected override void OnPreRender(EventArgs e)

        {

            base.OnPreRender(e);
              SPRibbon current = SPRibbon.GetCurrent(this);

              if (current != null)

              {

                  current.CommandUIVisible = true;

                  current.MakeTabAvailable("Ribbon.Read");

                  current.MakeTabAvailable("Ribbon.Test");

                  current.InitialTabId = "Ribbon.Test";

                  current.Minimized = false;

                  SPRibbonScriptManager manager = new SPRibbonScriptManager();

                  List<IRibbonCommand> commands = new List<IRibbonCommand>();

                  bool admin = base.GlobalAdmin.IsCurrentUserMachineAdmin();
                  commands.Add(new SPRibbonCommand("TestTab"));

                  commands.Add(new SPRibbonCommand("TestG1Group"));

                  commands.Add(new TestCommand("Test_New", "test.aspx", TestNavigateOptions.AppendSelectionIdParameter, admin));

                  commands.Add(new TestCommand("Test_Extend", "extendwebfarm.aspx?WebApplicationId=", TestNavigateOptions.AppendSelectionIdParameter | TestNavigateOptions.ModalDialog, admin));

                  commands.Add(new TestCommand("Test_DeleteTop", "DeleteWebApplication.aspx?WebApplicationId=", TestNavigateOptions.DisableForCentralAdmin | TestNavigateOptions.AppendSelectionIdParameter | TestNavigateOptions.ModalDialog, admin));

                  commands.Add(new TestCommand("Test_Delete", "DeleteWebApplication.aspx?WebApplicationId=", TestNavigateOptions.DisableForCentralAdmin | TestNavigateOptions.AppendSelectionIdParameter | TestNavigateOptions.ModalDialog | TestNavigateOptions.Privileged,
admin));

                  commands.Add(new TestCommand("Test_DeleteIIS", "unextendvs.aspx?WebApplicationId=", TestNavigateOptions.DisableForCentralAdmin | TestNavigateOptions.AppendSelectionIdParameter | TestNavigateOptions.ModalDialog | TestNavigateOptions.Privileged,
admin));

                  commands.Add(new SPRibbonCommand("TestG2Group"));

                  commands.Add(new TestCommand("Test_SettingsTop", "vsgeneralsettings.aspx?type=gen&WebApplicationId=", TestNavigateOptions.AppendSelectionIdParameter | TestNavigateOptions.ModalDialog, admin));

                  commands.Add(new TestCommand("Test_Settings", "vsgeneralsettings.aspx?type=gen&WebApplicationId=", TestNavigateOptions.AppendSelectionIdParameter | TestNavigateOptions.ModalDialog, admin));

                  commands.Add(new TestCommand("Test_ResourceThrottling", "vsgeneralsettings.aspx?type=res&WebApplicationId=", TestNavigateOptions.AppendSelectionIdParameter | TestNavigateOptions.ModalDialog, admin));

                  commands.Add(new TestCommand("Test_Workflow", "workflowAdmin.aspx?WebApplicationId=", TestNavigateOptions.AppendSelectionIdParameter | TestNavigateOptions.ModalDialog, admin));

                  commands.Add(new TestCommand("Test_Outgoing", "VSEmail.aspx?WebApplicationId=", TestNavigateOptions.AppendSelectionIdParameter | TestNavigateOptions.ModalDialog, admin));

                  commands.Add(new TestCommand("Test_OutgoingXms", "VSXms.aspx?WebApplicationId=", TestNavigateOptions.AppendSelectionIdParameter | TestNavigateOptions.ModalDialog, admin));

                  commands.Add(new TestCommand("Test_Features", "ManageWebAppFeatures.aspx?WebApplicationId=", TestNavigateOptions.AppendSelectionIdParameter | TestNavigateOptions.ModalDialog, admin));

                  commands.Add(new TestCommand("Test_Paths", "scprefix.aspx?WebApplicationId=", TestNavigateOptions.AppendSelectionIdParameter | TestNavigateOptions.ModalDialog, admin));

                  commands.Add(new TestCommand("Test_Connections", "ApplicationAssociationsDialog.aspx?Editable=false&ApplicationId=", TestNavigateOptions.AppendSelectionIdParameter | TestNavigateOptions.ModalDialog, admin));

                  commands.Add(new TestCommand("Test_SharePointDesigner", "SharePointDesignerAdmin.aspx?WebApplicationId=", TestNavigateOptions.AppendSelectionIdParameter | TestNavigateOptions.ModalDialog, admin));
                  manager.RegisterGetCommandsFunction(this, "getGlobalCommands", commands);

                  manager.RegisterCommandEnabledFunction(this, "commandEnabled", commands);

                  manager.RegisterHandleCommandFunction(this, "handleCommand", commands);
 
                  Type type = manager.GetType();

                  MethodInfo mi = type.GetMethod("RegisterPageComponent", BindingFlags.Instance | BindingFlags.NonPublic);

                  mi.Invoke(manager, new object[] { this, "SP.UI.Admin.js", "/_layouts/SP.UI.Admin.js", "SP.UI.Admin.TestPageComponent" });




              }

        }
 
由于上面的JS代码我是直接写在SP.UI.Admin.js中的,如果是自己单独写的JS文件,将这个文件名换掉即可。
 
之前并不清楚Ribbon的Tab可以在Feature中添加,不需要修改SharePoint的TEMPLATE/GLOBAL/XML/CMDUI.XML文件
 
在Feature中定义:
 <?xml version="1.0" encoding="utf-8"
?>

-
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
-
<CustomAction Id="TestRibbon"
Location="CommandUI.Ribbon" Sequence="100"
Title="Test">
-
<CommandUIExtension>
-
<CommandUIDefinitions>
-
<CommandUIDefinition Location="Ribbon.Tabs._children">
-
<Tab Id="Ribbon.Test"
Sequence="1300" Command="TestTab"
Title="Test" Description="First
Ribbon Test
">
- ......
) 
</Tab>
 
</CommandUIDefinition>
 
</CommandUIDefinitions>
 
</CommandUIExtension>
 
</CustomAction>
 
</Elements>
安装Feature后即可像之前一样使用。
 
另外Ribbon对应的JS方法可以直接写在页面上,然后将代码改成:
mi.Invoke(manager, new object[] { this, "SP.UI.Admin.js", "/_layouts/SP.UI.Admin.js", "SP.UI.Admin.WebApplicationPageComponent" });
 
也就不用重新写那些JS方法,只需将下面两个方法加入ASPX页面中。
function TestIsEnabled(a) {

        return true;

    }

    function TestNavigate(a, b, c) {

        var b = SP.UI.$create_DialogOptions();

        b.url = a;

        SP.UI.ModalDialog.showModalDialog(b)

    }
熟悉SharePoint2010的人都知道,SharePoint会弹出一个窗体来单独显示一个页面, SP.UI.ModalDialog.showModalDialog就是这个功能的实现的JS代码。想更详细了解,可以访问:http://blogs.msdn.com/jfrost/default.aspx
这样的好处是可以少写一些JS代码。而核心部分PageComponent还是直接使用SharePoint的JS类。

SharePoint 2010 Ribbon的实现的更多相关文章

  1. SharePoint 2010 Ribbon with wrong style in Chrome and Safari

    When we add custom ribbon to SharePoint 2010, it may display well in IE but not in Chrome and Safari ...

  2. How to: Hide the Ribbon in SharePoint 2010

    转:http://blogs.msdn.com/b/sharepointdev/archive/2012/04/30/how-to-hide-the-ribbon-in-sharepoint-2010 ...

  3. [转]SharePoint 2010 Download as Zip File Custom Ribbon Action

    在SharePoint 2010文档库中,结合单选框,在Ribbon中提供了批量处理文档的功能,比如,批量删除.批量签出.批量签入等,但是,很遗憾,没有提供批量下载,默认的只能一个个下载,当选择多个文 ...

  4. Sharepoint 2010 根据用户权限隐藏Ribbon菜单(利用css)

    本文介绍的是根据用户权限隐藏整个Ribbon菜单项. 操作环境:Windows Server 2008 r2+ SharePoint 2010 1.关于SharePoint  权限详细请参考:http ...

  5. sharepoint 2010 显示和隐藏Ribbon区域条

    在sharepoint 2010的页面中,我们在页面的最上方,有一条深灰色的Ribbon工具栏,如下图,这里可以通过下面的脚本,做一些脚本,来控制它的隐藏和显示. 最后把这些脚本,放在v4.maste ...

  6. sharepoint 2010 怎样在Ribbon区加入功能button

    继续前面的一篇博客,sharepoint 2010 怎样在列表中加入功能菜单操作项.这次主要是记录下,在Ribbon区域加入功能button.比如加入收藏button.例如以下图所看到的: 1. 还是 ...

  7. SharePoint 2010 master page 控件介绍(2):ribbon (一同事读听着像泪奔)

    转:http://blog.csdn.net/lgm97/article/details/6409208 <!-- =====  开始Ribbon ======================= ...

  8. Upgrade from SharePoint 2010 to SharePoint 2016

    [转]http://nikcharlebois.com/upgrade-from-sharepoint-2010-to-sharepoint-2016/ In this blog, I will go ...

  9. 使用SharePoint 2010 母版页

    SharePoint 2010母版页所用的还是ASP.NET 2.0中的技术.通过该功能,实现了页面框架布局与实际内容的分离.虽然在本质上自定义母版页的过程和以前版本的SharePoint大致相同,但 ...

随机推荐

  1. SQL Server2008数据库导入导出兼容性处理

    使用场景:SQL Server 的高版本数据库恢复到低版本则可能会有兼容性问题,为了解决此类问题进行数据库脚本模式处理,数据库结构,及数据存储更换版本等. 1.  选择要导出的数据库,右键任务,生成脚 ...

  2. PHP常见算法-面试篇(2)

    1.顺序查找 思路分析: 从数组的第一个元素开始一个一个向下查找,如果有和目标一致的元素,查找成功:如果到最后一个元素仍没有目标元素,则查找失败. 代码实现: <?php function se ...

  3. 挑战Python-20160826

    给你一字典a,如a={1:1,2:2,3:3},输出字典a的key,以','链接,如‘1,2,3'.

  4. Oracle 事务相关的一些测试

    1.sqlplus 客户端正常退出 SQL> desc t; 名称 是否为空? 类型 ----------------------------------------- -------- --- ...

  5. opengl混合效果

    效果如下图:

  6. Static File Middleware

    [ASP.NET Core] Static File Middleware   前言 本篇文章介绍ASP.NET Core里,用来处理静态档案的Middleware,为自己留个纪录也希望能帮助到有需要 ...

  7. python 下载安装及setuptools应用

    1.首先下载python安装程序,下载地址:https://www.python.org/download/releases/2.7.8/ 如下图: 因为我的机器是32位的就选择了Windows x8 ...

  8. ACM-进阶之路

    ACM进阶计划 ACM队不是为了一场比赛而存在的,为的是队员的整体提高. 大学期间,ACM队队员必须要学好的课程有: l C/C++两种语言 l 高等数学 l 线性代数 l 数据结构 l 离散数学 l ...

  9. Computer Vision的尴尬---by林达华

    Computer Vision的尴尬---by林达华 Computer Vision是AI的一个非常活跃的领域,每年大会小会不断,发表的文章数以千计(单是CVPR每年就录取300多,各种二流会议每年的 ...

  10. Design Tutorial: Make It Nondeterministic

    Codeforces Round #270:C;http://codeforces.com/contest/472 题意:水题 题解:贪心即可. #include<iostream> #i ...