Skip to main content
 
Go Search
Home
Categories
Bloggers
Ways and Locations to add a custom action to a Ribbon in SharePoint 2010 using Code.
By: Amol Ajgaonkar | Posted: October 19, 2009 at 11:12 AM

To add a button to the ribbon in SharePoint 2010 we use the SPUserCustomAction class.

  1. Get  hold of the SPList object whose ribbon you want to customize.
  2. Access the UserCustomActions property and create a new instance of the SPUserCustomAction class.
  3. Set the appropriate properties of the new instance.
  4. Call update on the SPUserCustomAction instance.

There are different locations where the button will be added depending on the properties being set.

If you don’t set the CommandUIExtension property then the button gets added to the toolbar.

image

If you set the CommandUIExtension property the button shows up on the ribbon. The size of the button depends on the TemplateAlias attribute of the xml.

image

To set the CommandUIExtension property, an xml snippet needs to be created. Here is an example of the xml

<CommandUIExtension>
  <CommandUIDefinitions>
    <CommandUIDefinition
    Location='Ribbon.Library.Actions.Controls._children'>
      <Button Id='Ribbon.Library.Actions.NewRibbonButton'
      Command='NewRibbonButtonCommand'
      Image16by16='/_layouts/images/FILMSTRP.GIF'
      Image32by32='/_layouts/images/PPEOPLE.GIF'
      LabelText='Move Document'
      TemplateAlias='o2' />
    </CommandUIDefinition>
  </CommandUIDefinitions>
  <CommandUIHandlers>
    <CommandUIHandler
    Command='NewRibbonButtonCommand'
    CommandScript='javascript:alert('This is a new button!');' />
  </CommandUIHandlers>
</CommandUIExtension>
 

The TemplateAlias attribute in the xml defines how the button looks on the ribbon. Setting it to “o1” would use the 32x32 icon and setting it to “o2” would use the 16x16 icon.

Here is a code snippet.

 using (SPSite site = new SPSite("http://moss2010base:10522"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList list = web.Lists["test"];
                    SPUserCustomAction btnCustomRibbon =  list.UserCustomActions.Add();
                    btnCustomRibbon.CommandUIExtension = @"<CommandUIExtension>
  <CommandUIDefinitions>
    <CommandUIDefinition
    Location='Ribbon.Library.Actions.Controls._children'>
      <Button Id='Ribbon.Library.Actions.NewRibbonButton'
      Command='NewRibbonButtonCommand'
      Image16by16='/_layouts/images/FILMSTRP.GIF'
      Image32by32='/_layouts/images/PPEOPLE.GIF'
      LabelText='Move Document'
      TemplateAlias='o2' />
    </CommandUIDefinition>
  </CommandUIDefinitions>
  <CommandUIHandlers>
    <CommandUIHandler
    Command='NewRibbonButtonCommand'
    CommandScript='javascript:alert('This is a new button!');' />
  </CommandUIHandlers>
</CommandUIExtension>";
                    btnCustomRibbon.Name = "MoveDocument";
                    btnCustomRibbon.RegistrationType = SPUserCustomActionRegistrationType.List;
                    btnCustomRibbon.Title = "Move Document";
                    btnCustomRibbon.Location = Microsoft.SharePoint.WebControls.SPRibbon.CustomCommandsId;
                    btnCustomRibbon.Update();
                    list.Update();

                }
            }

The locations strings are defined in the SPRibbon class defined in the Microsoft.SharePoint.WebControls namespace.

This snippet will create the button on the ribbon. I will update the code snippet with a few changes to enhance the code.


  Comments   Add Comment   Share It  
  Your Name:
  Your Email: **will not be displayed
  Comment Title:
* Comments:
  If you cannot read the code, please
click here to get a new one. You won't
lose your comments by doing so.
* Security Code:
   
  
  
* Your Name:
* Your Email: **will not be displayed
* Recipient's Email:
* Subject:
  If you cannot read the code, please
click here to get a new one. You won't
lose your comments by doing so.
* Security Code:
  
  
  
 

 About

 [more]

 ‭(Hidden)‬ Admin Links

 External Links

 Tag Cloud