Binding a contextual tab to my web part

Add-in Express™ Support Service
That's what is more important than anything else

Binding a contextual tab to my web part
Show a contextual tab when my web part is active and hide it when the web part is not active 
This forum has moved to a new location. From now on, please post all your questions about Ribbon Designer for SharePoint on this forum.
Alver R.




Posts: 5
Joined: 2012-01-11
I have another question. Your manual shows how to display a contextual tab when a page contains a web part. But this is not what I want. I want to show my tab only when the web part becomes active, not when a page is loaded. How can I do this? Any sample please?
Posted 19 Jan, 2012 04:04:33 Top
Sergey Grischenko


Add-in Express team


Posts: 7233
Joined: 2004-07-05
Hi Alver,

Please try to use the code below. To display a contextual group you need to implement the IWebPartPageComponentProvider interface and provide information about the contextual group and tab which should be displayed when the web part gains the focus.
Also, you can download the 'ContextualGroupsExample' example http://www.add-in-express.com/files/projects_pub/sp_contextualgroupsexample.zip.


    public class MyContextRibbonUIWebPart : WebPart, IWebPartPageComponentProvider
    {
        private const string _ascxPath = @"~/_CONTROLTEMPLATES/ContextualGroupsExample/MyContextRibbonUIWebPart/MyContextRibbonUIWebPartUserControl.ascx";

        protected override void CreateChildControls()
        {
            Control control = Page.LoadControl(_ascxPath);
            Controls.Add(control);
        }

        public WebPartContextualInfo WebPartContextualInfo
        {
            get
            {
                string ribbonDesignerUniquePrefix = 
                    typeof(Features.MyContextRibbonUI.MyContextRibbonUI).FullName.Replace(".", String.Empty);
                WebPartContextualInfo info = new WebPartContextualInfo();
                WebPartRibbonContextualGroup contextualGroup = new WebPartRibbonContextualGroup();
                WebPartRibbonTab ribbonTab = new WebPartRibbonTab();
                contextualGroup.Id = "Ribbon." + ribbonDesignerUniquePrefix + "_" + "adxspRibbonContextualGroup3";
                contextualGroup.Command = "Ribbon." + ribbonDesignerUniquePrefix + "_" + "adxspRibbonContextualGroup3Command";
                ribbonTab.Id = "Ribbon." + ribbonDesignerUniquePrefix + "_" + "adxspRibbonTab4";
                info.ContextualGroups.Add(contextualGroup);
                info.Tabs.Add(ribbonTab);
                info.PageComponentId = SPRibbon.GetWebPartPageComponentId(this);
                return info;
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            AddinExpress.SharePoint.ADXSPRibbon.ShowContextualGroup(this.Page, typeof(Features.MyContextRibbonUI.MyContextRibbonUI), "adxspRibbonContextualGroup2");
            base.OnPreRender(e);
        }
    }
Posted 19 Jan, 2012 08:15:44 Top
Alver R.




Posts: 5
Joined: 2012-01-11
Hi Sergey,

It worked, thank you!
Posted 19 Jan, 2012 10:38:43 Top