自定义 Web 部件添加上下文功能区选项卡时出现问题
请按照 http://blog 中所示的示例操作.dennus.net/2010/07/20/ribbon-buttons-with-postback-in-sp2010/,我在此示例中没有使用声明性方法,而是创建了自定义 Web 部件类并注册了功能区数据扩展 pageComponent OnPreRender 事件期间的脚本等。
一切似乎都工作得很好,当显示 Web 部件页面时,我得到了呈现的上下文组/选项卡/控件。但是,如果我在同一页面上添加两个自定义 Web 部件,则会遇到错误:ArgumentException:项目已添加。字典中的键:'Ribbon.MyContextualTabGroup' 添加键:'Ribbon.MyContextualTabGroup']。
我怎样才能避免这个问题?最好的方法是什么?理想情况下,当选择页面上的不同自定义 Web 部件时,我希望功能区上下文组/选项卡/控件以不同方式呈现,但我错过了该部件在 SharePoint 2010 功能区框架中的工作方式。
为了检测页面上的第一个 Web 部件可能已注册上下文组,我尝试了 SPRibbon.IsTabAvailable(tabID),但这总是返回 true - 即使在我实际添加选项卡 XML 数据扩展之前也是如此。我很困惑。注意我没有使用自定义操作/功能部署方法,而是动态注册功能区数据扩展和页面组件。
下面的代码片段显示了我如何添加功能区:
Microsoft.Web.CommandUI.Ribbon ribbon = SPRibbon.GetCurrent(theWebPart.Page);
if (ribbon != null)
{
// register data extension
XmlDocument ribbonExtensions = new XmlDocument();
ribbonExtensions.LoadXml(ContextualGroupInfo.ToXml());
ribbon.RegisterDataExtension(ribbonExtensions.FirstChild,
"Ribbon.ContextualTabs._children");
// Register initialize function
var manager = new SPRibbonScriptManager();
var methodInfo = typeof(SPRibbonScriptManager).GetMethod(
"RegisterInitializeFunction",
System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
methodInfo.Invoke(manager, new object[]
{
webPart.Page,
"InitPageComponent",
"/_layouts/PageComponent.js",
false,
"RibbonCustomization.PageComponent.initialize()"
});
var commands = new List();
commands.Add(new SPRibbonCommand(ContextualGroup.EnableContextualGroupCommand));
commands.Add(new SPRibbonCommand(ContextualTab.EnableContextualTabCommand));
...
// add ribbon button commands
...
// initialize tab
ribbon.Minimized = false;
ribbon.CommandUIVisible = true;
if (!ribbon.IsTabAvailable(ContextualTab.ID))
{
ribbon.MakeTabAvailable(ContextualTab.ID);
ribbon.EnableVisibilityContext(ContextualTab.VisibilityContext);
ribbon.MakeContextualGroupInitiallyVisible(ContextualTabGroupID, string.Empty);
ribbon.NormalizeContextualGroup(ContextualTabGroupID, string.Empty);
}
// Make the tab active by default when the page is opened.
ribbon.InitialTabId = ContextualTab.ID;
...
}
Follow the example illustrated in http://blog.dennus.net/2010/07/20/ribbon-buttons-with-postback-in-sp2010/, rather than using declarative approach in this example, I created custom web part class and registered ribbon data extension, pageComponent script and etc. during OnPreRender event.
Everything appears to be working just fine, I got my contextual group/tab/control rendered when web part page is displayed. However, if I add two of my custom web parts on the same page, I ran into error: ArgumentException: Item has already been added. Key in dictionary: 'Ribbon.MyContextualTabGroup' Key being added: 'Ribbon.MyContextualTabGroup'].
How can I avoid this problem? What is the best approach? Ideally I would like the ribbon contextual group/tab/control render differently when different custom web part on the page is selected but I missed how that part works in SharePoint 2010 Ribbon framework.
In an attempt to detect that the first web part on the page may have registered the contextual group, I tried SPRibbon.IsTabAvailable(tabID) but this always returns me true - even before I actually added the tab XML data extension. I am quite confused. Note I did not use the Custom Action/feature deployment approach but dynamically register ribbon data extension and page component.
Below are code snippet showing how I added the ribbon:
Microsoft.Web.CommandUI.Ribbon ribbon = SPRibbon.GetCurrent(theWebPart.Page);
if (ribbon != null)
{
// register data extension
XmlDocument ribbonExtensions = new XmlDocument();
ribbonExtensions.LoadXml(ContextualGroupInfo.ToXml());
ribbon.RegisterDataExtension(ribbonExtensions.FirstChild,
"Ribbon.ContextualTabs._children");
// Register initialize function
var manager = new SPRibbonScriptManager();
var methodInfo = typeof(SPRibbonScriptManager).GetMethod(
"RegisterInitializeFunction",
System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
methodInfo.Invoke(manager, new object[]
{
webPart.Page,
"InitPageComponent",
"/_layouts/PageComponent.js",
false,
"RibbonCustomization.PageComponent.initialize()"
});
var commands = new List();
commands.Add(new SPRibbonCommand(ContextualGroup.EnableContextualGroupCommand));
commands.Add(new SPRibbonCommand(ContextualTab.EnableContextualTabCommand));
...
// add ribbon button commands
...
// initialize tab
ribbon.Minimized = false;
ribbon.CommandUIVisible = true;
if (!ribbon.IsTabAvailable(ContextualTab.ID))
{
ribbon.MakeTabAvailable(ContextualTab.ID);
ribbon.EnableVisibilityContext(ContextualTab.VisibilityContext);
ribbon.MakeContextualGroupInitiallyVisible(ContextualTabGroupID, string.Empty);
ribbon.NormalizeContextualGroup(ContextualTabGroupID, string.Empty);
}
// Make the tab active by default when the page is opened.
ribbon.InitialTabId = ContextualTab.ID;
...
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
听起来您正在将 TabGroup 两次添加到功能区集合中的某个位置。始终让您的添加代码被检查包围,以查看选项卡组是否已添加。
这就是你的添加代码的样子吗?
It sounds like you are adding a TabGroup twice to the ribbon collection somewhere. Always have your add code surrounded by a check to see if the tab group has already been added.
Is that what your add code looks like?
我最终使用数据扩展 XML 和自定义 Web 部件生成唯一的上下文组 ID 和序列,以避免此错误。然而,当两个 Web 部件添加到页面时,这会向功能区添加两个上下文组/选项卡。
I end up generating unique contextual group ID and sequence with the data extension XML with the custom web part to avoid this error. This however added two contextual group/tab to the Ribbon when two web parts are added to the page.