Customizing the SharePoint Ribbon: C# sample
If you’ve used SharePoint, I’m sure the first thing you would’ve noticed is the Ribbon UI. The SharePoint ribbon provides users with a familiar Office user interface and it is also extensible for developers. This means developers can add their own tabs, groups and controls to the ribbon.
In this post I’ll show you how to create your own simple tab and elements for the tab in SharePoint 2010. If your pc is not set up for SharePoint 2010 development yet, read my last post Setting up your PC for SharePoint 2010 development.
Let’s get started by creating a new Empty SharePoint Project in Visual Studio 2010. You can find the project template under Visual C# > SharePoint > 2010.
The SharePoint Customization Wizard will start. Validate the site address to make sure you’re using the correct site for debugging and select Deploy as a sandboxed solution. Selecting this option will allow us to deploy this customization to Office 365 as well as a local instance of SharePoint.
We’ll add our own tab to any page that contains a list view, to do this we need to add a feature. To add a feature right-click on the Features folder and select Add Feature.
Change the feature title to My Custom Ribbon.
In the Solution Explorer select Feature1 in the Features folder and rename it to MyRibbon. The solution explorer should now look like this:
Next add a new Empty Element item to the project and call it MyCustomRibbonTab.
After you’ve added the Empty Element, the Elements.xml file will open and the Solution Explorer should look like this:
The Elments.xml file will be used to define the Ribbon Tab layout. But I have to warn you that the xml can turn into a bit of a nightmare. First, we need to define an extension to the user interface. To do this you’ll add a CustomAction section to the xml file.
<CustomAction Id="ADXSampleRibbonTab" Location="CommandUI.Ribbon.ListView" RegistrationId="101" RegistrationType="List"> </CustomAction>
The Id attribute is not required, however, it is a good idea to include it as it is used to uniquely identify the custom action. Location specifies the location for the custom action – in this case it will be on any Listview pages’ ribbon. You can see a list of custom action locations in the SharePoint 2010 SDK or on MSDN. The RegistrationType attribute specifies the registration attachment for per item actions. The possible values for it are:
The following mark-up will add a ribbon tab with two buttons, two textboxes as well as logic that will show a modal dialogue with the SharePoint calendar when the user clicks the one button. And if the user clicks the other button, the total numbers in the two textboxes will be displayed.
As you can see it takes a lot of XML to generate just a few elements. You’ll notice two <CommandUIDefinition> sections where one is used to specify the Tab, it’s scaling, groups and controls. It has a relatively easy hierarchy to understand which you will recognise as it is similar to Microsoft Office’s ribbon. The <Scaling> section is used to specify how the tab will resize and display on different resolutions.
It is important that each control has a unique ID, I found the best way to do this is using a namespace approach e.g. Ribbon.ADXSampleTab.CalendarGroup.ShowCalendar. You’ll also notice that the button controls have a Command attribute. This command is declared further down in the XML in the <CommandUIHandlers> section.
Once the XML is in place, you can build and run the project. In your SharePoint test site navigate to any list page (for example a document library) and you should see the custom tab.
When you click on the Show Calendar button a modal dialog will be displayed with the SharePoint Calendar.
Deploying the SharePoint ribbon to Office 365
Now that we have our solution we need to deploy it to Office 365. Visual Studio makes this very easy, all you need to do is right-click the project in Solution Explorer and choose Package. This will create a .wsp file which you would need to upload to Office 365. To do this, log into your Office 365 account and navigate to the Site Settings page and click on Solutions under Galleries
Click the Upload Solution button on the Solutions tab and upload the .wsp file. Once it is uploaded you need to activate it by selecting it in the list of solutions and clicking on the Activate button. If all went well, you should see your new tab when browsing to a page that contains a list.
Phew! That took some doing. I have barely scratched the surface of what can be done with the ribbon but I suggest you work through the SharePoint SDK as well as some examples available on the web.
Thank you for reading. Until next time, keep coding!