Adding a custom tab to SharePoint Ribbon

Back to Walkthrough

This topic describes how to add a custom tab to the SharePoint Rbbon by using custom action. In this walkthrough we'll add simple ribbon tab to all lists for all list item' forms on a web.

Defining the tab

var myRibbonTab = new RibbonTab("TabId", "TabTitle")
{
    Sequence = 1001,
    Groups = new[]
    {
        new RibbonGroup("GroupId", "GroupTitle")
        {
            Sections = new[]
            {
                new RibbonGroupSection
                {
                    Type = RibbonSectionType.OneRow,
                    DisplayMode = RibbonDisplayMode.Image32AndCaption,
                    Controls = new RibbonControl[]
                    {
                        new RibbonButton("ButtonId", "ButtonTitle")
                        {
                            Image = Gallery2.GetExternalDataFromText,
                            OnClientClick = "alert('GetExternalDataFromText')"
                        }
                    }
                }
            }
        }
    }
};

Add feature receivers

FeatureActivated to provision custom ribbon tab:
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
    // Web-scoped feature
    // Getting current web
    var web = properties.Feature.Parent as SPWeb;

    // Customization' id.
    var featureId = new Guid("fd22872d-7565-4491-9124-296c734fecd5");

    // New RibbonCustomAction instance
    var ca = new RibbonCustomAction();
    // Provision tab defined in the listing bellow
    // All lists and all forms
    ca.Provision(featureId, web, ListTypes.All, ListForms.All);
}


Result after feature activation:
Custom ribbon tab

FeatureDeactivating to remove our custom action (ribbon tab):
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
    var web = properties.Feature.Parent as SPWeb;
    var featureId = new Guid("fd22872d-7565-4491-9124-296c734fecd5");
    RibbonCustomAction.RemoveAllCustomizations(web, featureId);
}


Result after feature deactivation:

See also

Walkthrough

Adding a custom tab to the SharePoint Rbbon




Last edited Aug 31, 2013 at 10:12 PM by VitalyZhukov, version 2

Comments

No comments yet.