自定义 Web 部件添加上下文功能区选项卡时出现问题

发布于 2024-10-14 07:22:17 字数 2424 浏览 8 评论 0原文

请按照 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

楠木可依 2024-10-21 07:22:17

听起来您正在将 TabGroup 两次添加到功能区集合中的某个位置。始终让您的添加代码被检查包围,以查看选项卡组是否已添加。

if (!ribbon.IsTabAvailable(yourid)) {
    ribbon.MakeTabAvailable(yourid);
}

这就是你的添加代码的样子吗?

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.

if (!ribbon.IsTabAvailable(yourid)) {
    ribbon.MakeTabAvailable(yourid);
}

Is that what your add code looks like?

女中豪杰 2024-10-21 07:22:17

我最终使用数据扩展 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文