SharePoint 中的多个 Web 部件和功能区

发布于 2024-11-16 08:26:48 字数 2408 浏览 9 评论 0原文

我正在将功能区关联到我的网络部件。我需要在一个页面中添加两个以上的 Web 部件。

我不想为每个 Web 部件设置单独的上下文组/选项卡。有没有办法检查页面当前功能区中是否存在特定组/选项卡?

此时,当我向页面添加多个 Web 部件时,我收到以下错误:

Item 已添加。字典中的键:'Ribbon.MyContextualTabGroup' 添加的键:'Ribbon.MyContextualTabGroup'

这是我的代码供您参考:

/// <summary>
/// Gets the web part contextual info.
/// </summary>
public WebPartContextualInfo WebPartContextualInfo
{
    get
    {
        var webPartContextualInfo = new WebPartContextualInfo();
        var webPartRibbonContextualGroup = new WebPartRibbonContextualGroup();
        var webPartRibbonTab = new WebPartRibbonTab();

        webPartRibbonContextualGroup.Id = "Ribbon.MyContextualTabGroup";
        webPartRibbonContextualGroup.Command = "MyContextualTab.EnableContextualGroup";
        webPartRibbonContextualGroup.VisibilityContext = "MyContextualTab.CustomVisibilityContext";

        webPartRibbonTab.Id = "Ribbon.MyTab";
        webPartRibbonTab.VisibilityContext = "MyContextualTab.CustomVisibilityContext";

        webPartContextualInfo.ContextualGroups.Add(webPartRibbonContextualGroup);
        webPartContextualInfo.Tabs.Add(webPartRibbonTab);
        webPartContextualInfo.PageComponentId = SPRibbon.GetWebPartPageComponentId(this);

        return webPartContextualInfo;
    }
}

/// <summary>
/// Adds the contextual tab.
/// </summary>
private void AddContextualTab()
{
    SPRibbon spRibbon = SPRibbon.GetCurrent(Page);

    if (spRibbon == null) return;

    var ribbonExtensions = new XmlDocument();

    ribbonExtensions.LoadXml(_contextualTab);
    spRibbon.RegisterDataExtension(ribbonExtensions.FirstChild, "Ribbon.ContextualTabs._children");

    ribbonExtensions.LoadXml(_contextualTabTemplate);
    spRibbon.RegisterDataExtension(ribbonExtensions.FirstChild, "Ribbon.Templates._children");
}

/// <summary>
/// The event handler for the System.Web.UI.Control.PreRender event that occurs immediately before the Web Part is rendered to the Web Part Page it is contained on.
/// </summary>
/// <param name="e">A System.EventArgs that contains the event data.</param>
protected override void OnPreRender(EventArgs e)
{
    base.OnPreRender(e);

    AddContextualTab();

    ClientScriptManager clientScriptManager = Page.ClientScript;
    clientScriptManager.RegisterClientScriptBlock(GetType(), "MyWebPart", DelayScript);
}

I am associating ribbon to my webpart. I have a need to add more than two webparts in a page.

I do not want to have a separate contextual group/tab for each webpart. Is there a way to check if a specific group/tab exists in the current ribbon on the page?

At this point, when I add more than one webpart to the page, I an getting the following error:

Item has already been added. Key in dictionary: 'Ribbon.MyContextualTabGroup' Key being added: 'Ribbon.MyContextualTabGroup'

Here is my code for your reference:

/// <summary>
/// Gets the web part contextual info.
/// </summary>
public WebPartContextualInfo WebPartContextualInfo
{
    get
    {
        var webPartContextualInfo = new WebPartContextualInfo();
        var webPartRibbonContextualGroup = new WebPartRibbonContextualGroup();
        var webPartRibbonTab = new WebPartRibbonTab();

        webPartRibbonContextualGroup.Id = "Ribbon.MyContextualTabGroup";
        webPartRibbonContextualGroup.Command = "MyContextualTab.EnableContextualGroup";
        webPartRibbonContextualGroup.VisibilityContext = "MyContextualTab.CustomVisibilityContext";

        webPartRibbonTab.Id = "Ribbon.MyTab";
        webPartRibbonTab.VisibilityContext = "MyContextualTab.CustomVisibilityContext";

        webPartContextualInfo.ContextualGroups.Add(webPartRibbonContextualGroup);
        webPartContextualInfo.Tabs.Add(webPartRibbonTab);
        webPartContextualInfo.PageComponentId = SPRibbon.GetWebPartPageComponentId(this);

        return webPartContextualInfo;
    }
}

/// <summary>
/// Adds the contextual tab.
/// </summary>
private void AddContextualTab()
{
    SPRibbon spRibbon = SPRibbon.GetCurrent(Page);

    if (spRibbon == null) return;

    var ribbonExtensions = new XmlDocument();

    ribbonExtensions.LoadXml(_contextualTab);
    spRibbon.RegisterDataExtension(ribbonExtensions.FirstChild, "Ribbon.ContextualTabs._children");

    ribbonExtensions.LoadXml(_contextualTabTemplate);
    spRibbon.RegisterDataExtension(ribbonExtensions.FirstChild, "Ribbon.Templates._children");
}

/// <summary>
/// The event handler for the System.Web.UI.Control.PreRender event that occurs immediately before the Web Part is rendered to the Web Part Page it is contained on.
/// </summary>
/// <param name="e">A System.EventArgs that contains the event data.</param>
protected override void OnPreRender(EventArgs e)
{
    base.OnPreRender(e);

    AddContextualTab();

    ClientScriptManager clientScriptManager = Page.ClientScript;
    clientScriptManager.RegisterClientScriptBlock(GetType(), "MyWebPart", DelayScript);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

时间你老了 2024-11-23 08:26:48

上下文功能区无法在 Web 部件的不同实例之间共享。因为仅当您的 Web 部件实例在页面上具有“焦点”时才会显示功能区。因此,Web 部件的多个实例必须创建自己的上下文组。

为了避免功能区 ID 重复,请将 Web 部件实例特定部件附加到功能区 ID。您可以使用 Web 部件的 ID

webPartRibbonContextualGroup.Id = "Ribbon.MyContextualTabGroup." + ID;
// ...
webPartRibbonTab.Id = "Ribbon.MyTab." + ID;
// etc.

Contextual ribbons cannot be shared between different instances of a web part. Since the ribbon will only be displayed if your web part instance has the "focus" on the page. Therefore several instances of the web part have to create their own contextual group.

To avoid ribbon ID duplication append a web part instance specific part to the ribbon IDs. You could use the web part's ID:

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