sharepoint-2010ribbon

Create SharePoint 2010 ribbon button programmatically w/o feature XML


I have to create a SharePoint 2010 ribbon element (tabs, buttons, groups, etc.). Is there a way to create such elements via the SharePoint API without using custom actions?

Edit: I've just found the possibilty to add elements like this (link):

SPRibbon r = SPRibbon.GetCurrent(Page);
r.RegisterDataExtension(/* XmlNode containing ribbon element code */);

Another workaround would be to append a custom web control and append dynamic controls to this "placeholder".

Is there a way to create elements without using xml nodes?


Solution

  • For layouts pages and webparts, you can create Ribbon without any XML, using Ribbon Utils for SharePoint 2010.

    For example, for layouts page, you will need to inherit from RibbonUtils.RibbonLayoutsPage and provide your definition of a ribbon.

    Code for the most simple page with custom ribbon tab & one button on it will look like this:

    public partial class MyRibbonPage : RibbonLayoutsPage
    {
        public override TabDefinition GetTabDefinition()
        {
            return new TabDefinition()
            {
                Id = "TestRibbon",
                Title = "Test",
                Groups = new GroupDefinition[]
                {
                    new GroupDefinition()
                    {
                        Id = "TestGroup",
                        Title = "Test group",
                        Template = GroupTemplateLibrary.SimpleTemplate,
                        Controls = new ControlDefinition[]
                        {
                            new ButtonDefinition()
                            {
                                Id = "TestButton",
                                Title = "Test button",
                                CommandJavaScript = "alert('test!');",
                                Image32Url = "/_layouts/images/lg_ICHLP.gif",
                            }
                        }
                    }
                }
    
            };
        }
    }
    

    You will find more examples and very good documentation on the project page on CodePlex.

    AFAIK, it is for now most simple and quick way to create ribbon programmatically for application pages & webparts.