转: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. LevelDb原理剖析

    在说LevelDb之前,先认识两位大牛,Jeff Dean和Sanjay Ghemawat,这两位是Google公司重量级的工程师,为数甚少的Google Fellow之二. Jeff Dean其人: ...

  2. linux下screen工具的简单使用

    有时候,希望即使退出终端了,下次登录linux的时候,还能回到程序的控制界面,这个时候,screen工具就很有用了例如,写了一个从控制台读取屏幕输入的程序input_test,如果从终端退出了,下次登 ...

  3. ubuntu12.10设置禁止锁屏和屏幕常亮

    1.System Settings -> Brightness and Lock -> Turn off screen... set to "Never" 进入ubun ...

  4. C#基础(七)——静态类与非静态类、静态成员的区别

    静态类 静态类与非静态类的重要区别在于静态类不能实例化,也就是说,不能使用 new 关键字创建静态类类型的变量.在声明一个类时使用static关键字,具有两个方面的意义:首先,它防止程序员写代码来实例 ...

  5. 10.MVC框架开发(Ajax应用)

    1.MVC自带的Ajax应用, 使用步骤: 第一步,引入js框架 <script src="../../Scripts/jquery-1.4.4.js" type=" ...

  6. 一步步学习NHibernate(2)——配置NHibernate的环境

    请注明转载地址:http://www.cnblogs.com/arhat 第二章 环境搭建 在上一章中,我们知道了NHibernate是用来干什么的了,那么今天在本章中,我们开始搭建NHibernat ...

  7. iOS实现地图半翻页效果--老代码备用参考

    // Curl the image up or down CATransition *animation = [CATransition animation]; [animation setDurat ...

  8. noj [1475] Bachelor (找1的个数)

    http://ac.nbutoj.com/Problem/view.xhtml?id=1475 [1475] Bachelor 时间限制: 1000 ms 内存限制: 65535 K 问题描述 炎热的 ...

  9. 使用PyInstaller打包Python程序

    本文转载自: http://www.pycoding.com/2015/04/23/pyinstaller.html

  10. python multiprocessing 多进程

    ''' 如果要启动大量的子进程,可以用进程池的方式批量创建子进程: ''' def test_task(name): print 'Run task %s (%s)...' % (name, os.g ...