博客地址 http://blog.csdn.net/foxdave

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的更多相关文章

  1. 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 ...

  2. [转]Creating a custom ribbon for Outlook 2013, 2010 and toolbar for Outlook 2007, 2003 – C# sample

    本文转自:https://www.add-in-express.com/creating-addins-blog/2013/05/21/outlook-ui-customization-ribbons ...

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

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

  4. sharepoint 2010 页面添加footer方法 custom footer for sharepoint 2010 master page

    转:http://blog.csdn.net/chenxinxian/article/details/8720893 在sharepoint 2010的页面中,我们发现,没有页尾,如果我们需要给页面添 ...

  5. Upgrade custom workflow in SharePoint

    Experience comes when you give a try or do something, I worked in to many SharePoint development pro ...

  6. how to create a custom form for sharepoint list

    在VS中创建一个applicationPage映射到Layouts文件夹下,然后代码如下: SPList lstTest = web.Lists["Shared Documents" ...

  7. Visual Studio 2010 集成 SP1 补丁 制作 Visual Studio 2010 Service Pack 1 完整版安装光盘的方法

    Now that Visual Studio 2010 SP1 has been released, administrators and developers may wish to install ...

  8. Systemc在VC++2010安装方法及如何在VC++2010运行Noxim模拟器

    Systemc在VC++2010的安装方法可以参考文档"Systemc with Microsoft Visual Studio 2008.pdf".本文档可以在"htt ...

  9. Installshield 2010 中集成. Net framework4 与 vc++ 2010运行安装包

    1.prq的地址,通过以下地址,下载相应的prq文件 VC 2010 redist X86: http://saturn.installshield.com/is/prerequisites/micr ...

随机推荐

  1. 【Python】Python 打印和输出更多用法。

    Python 打印和输出 简述 在编程实践中,print 的使用频率非常高,特别是程序运行到某个时刻,要检测产生的结果时,必须用 print 来打印输出. 关于 print 函数,前面很多地方已经提及 ...

  2. 微软威胁情报中心总经理的十句话——From John Lambert——太精辟了.......

                                                      微软威胁情报中心总经理 John Lambert的十句话   1. What is the most ...

  3. 101-advanced-React易用性,概述

    React完全支持构建可访问的网站,通常使用标准的HTML技术. 1.可访问小部件 ARIA文档包含用于构建完全可访问的JavaScript小部件的技术. JSX完全支持所有aria- * HTML属 ...

  4. django-from

    构建一个表单 这是一个非常简单的表单.实际应用中,一个表单可能包含几十上百个字段,其中大部分需要预填充,而且我们预料到用户将来回编辑-提交几次才能完成操作. 我们可能需要在表单提交之前,在浏览器端作一 ...

  5. YTD易出现断层问题,请注意!

    declare @table table( company_id int ,--公司编号 quarter_num ),--季度 disti ),--分销商 num int --数量 ) insert ...

  6. 【android】开发笔记系列:行为篇

    1:键盘遮挡了输入框 在androidManifest.xml里,对应的activity里设置键盘模式 <activity android:name="活动名称" andro ...

  7. fiddler抓包HTTPS配置及代理设置

    使用fiddler抓包过程中遇到一系列的问题,浪费了大半天时间~~~写下解决办法 按照网上方法配置之后还是无法抓到cookies提示各种证书错误 1.卸载fiddler重新安装,设置 2.设置步骤 ( ...

  8. SQL学习笔记三(补充-3)之MySQL完整性约束

    阅读目录 一 介绍 二 not null与default 三 unique 四 primary key 五 auto_increment 六 foreign key 七 作业 一 介绍 约束条件与数据 ...

  9. UVA 11475 Extend to Palindrome(hash)题解

    题意:问你最少加几个字母使所给串变成回文串. 思路:一开始打算将正序和逆序都hash,然后用提取前缀后缀的方法来找,但是RE了,debug失败遂弃之.后来发现可以直接hash,一边hash一边比较.我 ...

  10. HDU 5934 Bomb(tarjan/SCC缩点)题解

    思路:建一个有向图,指向能引爆对象,把强连通分量缩成一点,只要点燃图中入度为0的点即可.因为入度为0没人能引爆,不为0可以由别人引爆. 思路很简单,但是早上写的一直错,改了半天了,推倒重来才过了... ...