Omicron Llama

Coding all day, every day.

Programmatically add Ribbon Custom Action to an Existing List

This is a cross-post from the original article from SharePoint365, available at: http://sp365.co.uk/2011/09/programmatically-add-ribbon-custom-action-to-an-existing-list/

There is plenty of documentation on the web for creating Ribbon Customisations using the standard SharePoint CAML XML mark-up. This is a great dialect for creating complex customisations to the ribbon, but they only get applied on Feature Activation.

What if you have a List in the site that already exists and you need to apply some Ribbon actions? Sure, you can add Ribbon extensions to a Content Type (and this is probably recommended), however not all of our environments that we encounter use Content Types. Or, we have Lists being created from the “Custom List” list definition (or list type) programmatically in a Feature Receiver (which is executed after all the Feature CAML has been loaded & provisioned). Or we have lists created dynamically from Workflows, or other Event Receivers, or some other dynamic method at runtime.

This little hint will show you how to add Ribbon customisations programmatically at any time. There is still XML involved, to define the actual Ribbon buttons and the commands they execute, but this can easily be added to an existing instance of a List in a site.

In essence, you add this XML to the SPList.CustomUIExtension property.

Beware that this might overwrite any other Ribbon customisations applied directly to this List (if there are any added using the aforementioned Feature CAML code), and thus might remove customisations already existing. If this is the case in your environment, ensure that you don’t blindly add the code to the property without first ‘getting’ it, and carefully injecting the XML you require before ‘setting’ the property with your full XML.

Here is an example, which gets a List that already exists in my site, and creates a link to Site Settings for a selected item in that list. Bit of a useless example, maybe, but like I said earlier, there’re other great examples of more complex and uses of Ribbon customisation. For brevity, I haven’t included an image in this either, but the examples on the web will show you how to include 32px and 16px images for your Ribbon Actions.

Leave a Reply

Your email address will not be published. Required fields are marked *