Custom Ribbon in SharePoint 2010 & which not wrok when migrate from 2010 to 2013

1. First of all, let me show you the ribbon modal in our project whcih just like the example from internet.

>>SPMIPRibbon.cs

I‘ve add some clear comments.

using System.Collections.Generic;
using System.Reflection;
using System.Web.UI;
using System.Xml;
using Microsoft.SharePoint.WebControls;

namespace Common
{
    public class SPMIPRibbon
    {
        //Ribbon definition template
        private const string contextualTabTemplate = "\r\n<GroupTemplate Id=\"Ribbon.Templates.Flexible2\">" +
                                                                                            "\r\n<Layout Title=\"LargeLarge\">" +
                                                                                            "\r\n<OverflowSection Type=\"OneRow\" TemplateAlias=\"o1\" DisplayMode=\"Large\"/>" +
                                                                                            "\r\n<OverflowSection Type=\"OneRow\" TemplateAlias=\"o2\" DisplayMode=\"Large\"/>" +
                                                                                            "\r\n</Layout>" +
                                                                                            "\r\n<Layout Title=\"LargeMedium\">" +
                                                                                            "\r\n<OverflowSection Type=\"OneRow\" TemplateAlias=\"o1\" DisplayMode=\"Large\"/>" +
                                                                                            "\r\n<OverflowSection Type=\"ThreeRow\" TemplateAlias=\"o2\" DisplayMode=\"Medium\"/>" +
                                                                                            "\r\n</Layout>" +
                                                                                            "\r\n<Layout Title=\"LargeSmall\">" +
                                                                                            "\r\n<OverflowSection Type=\"OneRow\" TemplateAlias=\"o1\" DisplayMode=\"Large\"/>" +
                                                                                            "\r\n<OverflowSection Type=\"ThreeRow\" TemplateAlias=\"o2\" DisplayMode=\"Small\"/>" +
                                                                                            "\r\n</Layout>" +
                                                                                            "\r\n<Layout Title=\"MediumLarge\">" +
                                                                                            "\r\n<OverflowSection Type=\"ThreeRow\" TemplateAlias=\"o1\" DisplayMode=\"Medium\"/>" +
                                                                                            "\r\n<OverflowSection Type=\"OneRow\" TemplateAlias=\"o2\" DisplayMode=\"Large\"/>" +
                                                                                            "\r\n</Layout>" +
                                                                                            "\r\n<Layout Title=\"MediumMedium\">" +
                                                                                            "\r\n<OverflowSection Type=\"ThreeRow\" TemplateAlias=\"o1\" DisplayMode=\"Medium\"/>" +
                                                                                            "\r\n<OverflowSection Type=\"ThreeRow\" TemplateAlias=\"o2\" DisplayMode=\"Medium\"/>" +
                                                                                            "\r\n</Layout>" +
                                                                                            "\r\n<Layout Title=\"MediumSmall\">" +
                                                                                            "\r\n<OverflowSection Type=\"ThreeRow\" TemplateAlias=\"o1\" DisplayMode=\"Medium\"/>" +
                                                                                            "\r\n<OverflowSection Type=\"ThreeRow\" TemplateAlias=\"o2\" DisplayMode=\"Small\"/>" +
                                                                                            "\r\n</Layout>" +
                                                                                            "\r\n<Layout Title=\"SmallLarge\">" +
                                                                                            "\r\n<OverflowSection Type=\"ThreeRow\" TemplateAlias=\"o1\" DisplayMode=\"Small\"/>" +
                                                                                            "\r\n<OverflowSection Type=\"OneRow\" TemplateAlias=\"o2\" DisplayMode=\"Large\"/>" +
                                                                                            "\r\n</Layout>" +
                                                                                            "\r\n<Layout Title=\"SmallMedium\">" +
                                                                                            "\r\n<OverflowSection Type=\"ThreeRow\" TemplateAlias=\"o1\" DisplayMode=\"Small\"/>" +
                                                                                            "\r\n<OverflowSection Type=\"ThreeRow\" TemplateAlias=\"o2\" DisplayMode=\"Medium\"/>" +
                                                                                            "\r\n</Layout>" +
                                                                                            "\r\n<Layout Title=\"SmallSmall\">" +
                                                                                            "\r\n<OverflowSection Type=\"ThreeRow\" TemplateAlias=\"o1\" DisplayMode=\"Small\"/>" +
                                                                                            "\r\n<OverflowSection Type=\"ThreeRow\" TemplateAlias=\"o2\" DisplayMode=\"Small\"/>" +
                                                                                            "\r\n</Layout>" +
                                                                                            "\r\n<Layout Title=\"Popup\" LayoutTitle=\"LargeMedium\" />" +
                                                                                            "\r\n</GroupTemplate>";

        /// <summary>
        /// Add Ribbon UI Method
        /// </summary>
        /// <param name="page">current control</param>
        /// <param name="tab">ribbon definition</param>
        /// <param name="tabID">ribbon tab id</param>
        /// <param name="isAvailable">is available</param>
        private static void AddRibbonTab(Page page, string tab, string tabID, bool isAvailable)
        {
            SPRibbon current = SPRibbon.GetCurrent(page);
            if (current != null)
            {
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(tab);
                current.RegisterDataExtension(doc.FirstChild, "Ribbon.Tabs._children");
                doc.LoadXml(contextualTabTemplate);
                current.RegisterDataExtension(doc.FirstChild, "Ribbon.Templates._children");
                current.Minimized = false;
                current.CommandUIVisible = true;
                if (!current.IsTabAvailable(tabID))
                {
                    current.MakeTabAvailable(tabID);
                }
                if (isAvailable)
                {
                    current.InitialTabId = tabID;
                }
            }
        }

        /// <summary>
        /// Add Ribbon Event Method
        /// </summary>
        /// <param name="page">current control</param>
        /// <param name="cmds">event detail</param>
        private static void AddTabEvents(Page page, List<IRibbonCommand> cmds)
        {
            SPRibbonScriptManager manager = new SPRibbonScriptManager();
            typeof(SPRibbonScriptManager).GetMethod("RegisterInitializeFunction", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(manager, new object[] { page, "InitPageComponent", "/_layouts/SCRIPTS/SPMIPRibbon.js", false, "SPMIPRibbon.PageComponent.initialize()" });
            manager.RegisterGetCommandsFunction(page, "getGlobalCommands", cmds);
            manager.RegisterCommandEnabledFunction(page, "commandEnabled", cmds);
            manager.RegisterHandleCommandFunction(page, "handleCommand", cmds);
        }

        /// <summary>
        /// Set Ribbon Method
        /// </summary>
        /// <param name="page">current control</param>
        /// <param name="tab">ribbon definition</param>
        /// <param name="tabID">ribbon tab id</param>
        /// <param name="cmds">event detail</param>
        /// <param name="isAvailable">is available</param>
        public static void Set(Page page, string tab, string tabID, List<IRibbonCommand> cmds, bool isAvailable)
        {
            AddRibbonTab(page, tab, tabID, isAvailable);
            AddTabEvents(page, cmds);
        }
    }
}

>>SPMIPRibbon.js

Ribbon command javascript file

function ULS_SP() {
    if (ULS_SP.caller) {
        ULS_SP.caller.ULSTeamName = "Windows SharePoint Services 4";
        ULS_SP.caller.ULSFileName = "SPMIPRibbon.js";
    }
}

Type.registerNamespace(‘SPMIPRibbon‘);

SPMIPRibbon.PageComponent = function () {
    ULS_SP();
    SPMIPRibbon.PageComponent.initializeBase(this);
}

SPMIPRibbon.PageComponent.initialize = function () {
    ULS_SP();
    ExecuteOrDelayUntilScriptLoaded(Function.createDelegate(null, SPMIPRibbon.PageComponent.initializePageComponent), ‘SP.Ribbon.js‘);
}

SPMIPRibbon.PageComponent.initializePageComponent = function () {
    ULS_SP();
    var ribbonPageManager = SP.Ribbon.PageManager.get_instance();
    if (null !== ribbonPageManager) {
        ribbonPageManager.addPageComponent(SPMIPRibbon.PageComponent.instance);
        ribbonPageManager.get_focusManager().requestFocusForComponent(SPMIPRibbon.PageComponent.instance);
    }
}

SPMIPRibbon.PageComponent.refreshRibbonStatus = function () {
    SP.Ribbon.PageManager.get_instance().get_commandDispatcher().executeCommand(Commands.CommandIds.ApplicationStateChanged, null);
}

SPMIPRibbon.PageComponent.prototype = {
    getFocusedCommands: function () {
        ULS_SP();
        return [];
    },
    getGlobalCommands: function () {
        ULS_SP();
        return getGlobalCommands();
    },
    isFocusable: function () {
        ULS_SP();
        return true;
    },
    receiveFocus: function () {
        ULS_SP();
        return true;
    },
    yieldFocus: function () {
        ULS_SP();
        return true;
    },
    canHandleCommand: function (commandId) {
        ULS_SP();
        return commandEnabled(commandId);
    },
    handleCommand: function (commandId, properties, sequence) {
        ULS_SP();
        return handleCommand(commandId, properties, sequence);
    }
}
SPMIPRibbon.PageComponent.registerClass(‘SPMIPRibbon.PageComponent‘, CUI.Page.PageComponent);
SPMIPRibbon.PageComponent.instance = new SPMIPRibbon.PageComponent();
NotifyScriptLoadedAndExecuteWaitingJobs("SPMIPRibbon.js");

Now let us see how to use it

Declare a ribbon definition like this as below

private string ribbonTab = @"
          <Tab Id=""SPMIPRibbon.Tab1"" Sequence=""400"" Description="""" Title=""清单采购一览"">
            <Scaling Id=""SPMIPRibbon.Scaling1"">
              <MaxSize Id=""SPMIPRibbon.MaxSize1"" Sequence=""10"" GroupId=""SPMIPRibbon.Group1"" Size=""LargeLarge""/>
              <Scale Id=""SPMIPRibbon.Scale1"" Sequence=""20"" GroupId=""SPMIPRibbon.Group1"" Size=""Popup"" />
            </Scaling>
            <Groups Id=""SPMIPRibbon.Groups1"">
              <Group Id=""SPMIPRibbon.Group1""
                     Sequence=""10""
                     Description=""""
                     Title=""操作区""
                     Image32by32Popup=""/_layouts/2052/images/formatmap32x32.png"" Image32by32PopupTop=""-416"" Image32by32PopupLeft=""-256""
                     Template=""Ribbon.Templates.Flexible2"" >
                <Controls Id=""SPMIPRibbon.Controls1"">
                <Button
                    Id=""SPMIPRibbon.Button2""
                    Sequence=""20""
                    Command=""SPMIPRibbon.Command2""
                    Image32by32=""/_layouts/2052/images/formatmap32x32.png"" Image32by32Top=""-128"" Image32by32Left=""-96""
                    LabelText=""编辑""
                    ToolTipTitle=""编辑采购清单""
                    ToolTipDescription=""编辑采购清单""
                    TemplateAlias=""o1""/>
                <Button
                    Id=""SPMIPRibbon.Button4""
                    Sequence=""30""
                    Command=""SPMIPRibbon.Command4""
                    Image32by32=""/_layouts/2052/images/formatmap32x32.png"" Image32by32Top=""-160"" Image32by32Left=""-416""
                    LabelText=""确认招标""
                    ToolTipTitle=""确认招标采购清单""
                    ToolTipDescription=""确认招标采购清单""
                    TemplateAlias=""o1""/>
                <Button
                    Id=""SPMIPRibbon.Button5""
                    Sequence=""30""
                    Command=""SPMIPRibbon.Command5""
                    Image32by32=""/_layouts/2052/images/formatmap32x32.png"" Image32by32Top=""-320"" Image32by32Left=""-224""
                    LabelText=""上传附件""
                    ToolTipTitle=""上传采购清单附件""
                    ToolTipDescription=""上传采购清单附件""
                    TemplateAlias=""o1""/>
                </Controls>
              </Group>
            </Groups>
          </Tab>";

Override OnPreRender method and add the following code

var cmds = new System.Collections.Generic.List<IRibbonCommand>();
            cmds.Add(new SPRibbonCommand("SPMIPRibbon.Command2", "gdv.GetSelectedFieldValues(‘ID;Qing_dlx;Chuang_jzh‘, Edit);", "CheckEditEnabled();"));
            cmds.Add(new SPRibbonCommand("SPMIPRibbon.Command5", "gdv.GetSelectedFieldValues(‘ID;Qing_dlx;Chuang_jzh‘, Upload);", "CheckEditEnabled();"));
            cmds.Add(new SPRibbonCommand("SPMIPRibbon.Command4", "gdv.GetSelectedFieldValues(‘ID;Qing_dlx;Que_rzhb;Chuang_jzh‘, Confrim);", "CheckEditEnabled();"));
            SPMIPRibbon.Set(Page, ribbonTab, "SPMIPRibbon.Tab1", cmds, true);

Enjoy it.

Here is one problem when we migrate it from SP14 to SP15, we may get error message as "getGlobalCommands not found".

The reason is in SharePoint 2013, the SPRibbonScriptManager class‘s execution is slower than the js execution, when the js object initializes, the needed commands have not generated.

To solve this, we need to do a little change to the js file.

Change the

ExecuteOrDelayUntilScriptLoaded(Function.createDelegate(null, SPMIPRibbon.PageComponent.initializePageComponent), ‘SP.Ribbon.js‘);

to

_spBodyOnLoadFunctionNames.push("SPMIPRibbon.PageComponent.initializePageComponent");

That is all, thanks.

Custom Ribbon in SharePoint 2010 & which not wrok when migrate from 2010 to 2013

时间: 2024-08-02 05:24:24

Custom Ribbon in SharePoint 2010 & which not wrok when migrate from 2010 to 2013的相关文章

Exchange Server 2010跨站点部署以及升级到Exchange Server 2013博文目录--持续更新

本博文提供Exchange Server 2010跨站点部署以及升级到Exchange Server 2013博文目录 01Exchange Server 2010跨站点部署-环境介绍 http://winteragain.blog.51cto.com/1436066/1661536 后续会持续更新新博文目录

过Postfix构建Exchange Server 2010邮件网关部署系列四:Exchange 2010典型安装

1.加载Exchange Server 2010 SP1安装光盘 2.在下图所示页面的步骤3中,选择"仅从DVD安装语言" 3.在下图所示页面中,单击"步骤4:--" 4.在下图所示页面中,单击"下一步"按钮: 5.在下图所示页面中,勾选"我接受许可协议中的条款" 6.在下图所示页面中,勾选"否",单击"下一步"按钮: 7.在下图所示页面中,选择"自定义Exchange Ser

Word 2010 入门教程【初步认识Word 2010】

(一)默认功能区介绍 默认功能区包括开始.插入.页面布局和引用等分组. (二)自定义功能区 通常在功能区中我们看到开始.插入.页面布局.引用.邮件.审阅和视图,那么如何再添加一些其他的功能区呢? 1. 文件 -----> 选项 ----- > 自定义功能区                 2. 在“自定义功能区”中勾选自己想添加的功能区,例如:开发工具. 3. 回到word主界面来看,自定义功能区"开发工具"添加成功. (三)快速访问工具栏 快速访问工具栏在功能区的上面,如

48Exchange 2010升级到Exchange 2013-图形化卸载2010邮箱服务器

19.4.5 移除EX2010数据库 移除DAG成员,点击删除按钮 点击完成,完成可用性组成员的删除 删除DAG 删除广州总部EX2010所有的邮箱数据库 下面数据库只剩下上海分支机构的了 19.4.6 移除EX2010邮箱服务器 19.4.6.1 GUI移除 下面先演示下GUI的方式进行卸载EX2010服务器,就类似我们平时卸载软件一样,在控制面板中,打开程序和功能,右键卸载 取消勾选邮箱角色和管理工具 这里我们没有删除OAB的缘故 删除OAB 重试OK 开始卸载 在ADSI下,可以看到EX0

打开Word 2010 老提示安装 Office single image 2010

解决办法: WScript.Echo "Try to repair registry key..."  'verify Office version  Set objshell = CreateObject("wscript.shell")  If version = 12 Then  objshell.Run "reg add HKCU\Software\Microsoft\Office\12.0\Word\Options /v NoReReg /t R

VSTO 学习笔记(十)Office 2010 Ribbon开发

原文:VSTO 学习笔记(十)Office 2010 Ribbon开发 微软的Office系列办公套件从Office 2007开始首次引入了Ribbon导航菜单模式,其将一系列相关的功能集成在一个个Ribbon中,便于集中管理.操作.这种Ribbon是高度可定制的,用户可以将自己常用的功能进行单独设置,提高工作效率.但是由于Office 2003时代用户的操作习惯已经养成,结果到了Office 2007很多菜单.按钮都找不到了,着实有些尴尬.经过一段时间的适应,相信大多数用户已经习惯Ribbon

使用SharePoint 2010 母版页

SharePoint 2010母版页所用的还是ASP.NET 2.0中的技术.通过该功能,实现了页面框架布局与实际内容的分离.虽然在本质上自定义母版页的过程和以前版本的SharePoint大致相同,但是从2007到2010仍然有许多重要的值得我们关注的变化.本文将试图: 巩固和掌握SharePoint 2010里母版页相关的一些基本操作 突出描述新版本和以前的版本之间的一些变化 了解一些有用的或比较常见的自定义方式 SharePoint 2010 母版页的类型 首先,让我们快速的看一下在Shar

在 Windows Vista、Windows 7 和 Windows Server 2008 上设置 SharePoint 2010 开发环境

适用范围: SharePoint Foundation 2010 | SharePoint Server 2010 本文内容 步骤 1:选择和预配置操作系统 步骤 2:安装 SharePoint 2010 的必备组件 步骤 3:安装 SharePoint 2010 步骤 4:安装 Visual Studio 2010 和开发人员文档 步骤 5:从已安装的系统创建 Hyper-V 映像 当您创建或自定义 SharePoint 解决方案时,通常最好在安装了 Microsoft SharePoint

[SharePoint 2010] SharePoint 2010上多人同時編輯Office 2010文件

Office 2010這個版本,提供了一個令人興奮的新功能,那就是它可以讓多人同時編輯一份Office 2010的文件. 這是一個很大的突破. 以往在與SharePoint搭配下的分享環境,檔案只能被簽出鎖定住,然後整個下載回Client端編輯,做完後再傳回去,文件簽入後其他人才能夠繼續編輯下去,這樣的方式造成了許多無謂的時間等待. 所以新一代Office 2010可以說大大改進了團隊協同運作的效率 Office 2010這個版本,提供了一個令人興奮的新功能,那就是它可以讓多人同時編輯一份Off