Microsoft SharePoint Developer Documentation Team Blog The Official Blog of the SharePoint Developer Documentation Team
(Before he left for a week of laying on various tropical beaches with drinks in his hand, Dallas asked me to post this for him. Since I'm stuck in the rainy Northwest and had nothing better to do, I was happy to oblige him.)
The ribbon has unified the command surface inside of SharePoint. It is now the primary point of entry for working with items inside of SharePoint. As such the ribbon is extensible through the use of declarative XML in a Feature.
This post will cover how to add a new tab to the ribbon in Microsoft SharePoint Foundation 2010. It will also, by necessity, cover how to create a group, controls, template, and scaling behavior. I will build this solution in Visual Studio 2010 Beta 2 and as a sandboxed solution for the ease of deployment.
The following will appear in a future version of the SDK. It is preliminary documentation and subject to change.
First, you will want to create a new project. Click on File, New, and then Project.
Under Visual C#, choose Empty SharePoint Project and enter AddARibbonTab for the Name. Click OK.
In the SharePoint Customization Wizard, choose Deploy as a sandboxed solution. Click Finish.
In the Solution Explorer, right click on Features and choose Add Feature.
Change the Title of the Feature to Custom Ribbon Tab. Also, rename Feature1 in the Solution Explorer to CustomRibbonTab.
Right click on the AddARibbonTab project in the Solution Explorer and choose Add, New Item…
In the Add New Item dialog, choose the Empty Element template. Enter CustomRibbonTab as the Name.
In the Elements.xml file, insert the following CustomAction element. The Location attribute tells the CustomAction where to apply the customization. The following list explains the acceptable values.
Customization appears everywhere for the specified RegistrationId.
Customization appears when the list view Web Part is present.
Customization appears on the edit form.
Customization appears on the new form.
Customization appears on the display form.
<?xml version="1.0" encoding="utf-8"?>
Add the following elements to define the ribbon extension and tab. The Location on the CommandUIDefinition element defines where the controls inside of it will render. In this example, you reference the Tabs collection of the ribbon. The _children convention tells the ribbon to insert the following XML, whether it is a tab, group, or control, into the collection of tabs. The Sequence attribute on the Tab element defines where the tab will render in regards to other tabs. The default tabs use multiples of 100, so the Sequence attribute should not be a multiple of 100 to prevent collisions.
Title="My Custom Tab"
Description="This holds my custom commands!"
When creating a custom tab, you must define how the tab will scale when controls are added. This is handled through the use of the Scaling element along with a GroupTemplate. The MaxSize element defines the maximum size of the controls in the group. The Scale element defines how the group will scale in different situations. The GroupId attribute associates a group with the scale size. The Size attribute is defined by the Layout element, which is defined later in this topic.
Now, you will define the groups of controls that will appear on the tab. The Group element contains the usual attributes along with a Template attribute. The Template attribute references the GroupTemplate that is defined later in this topic. The Controls element contains the controls that will render in the group. These can be buttons, dropdowns, and other control types. You can find a list in the SDK at this address: http://msdn.microsoft.com/en-us/library/ee537017(office.14).aspx. The controls inside the group must define the TemplateAlias and Command attributes. Like tabs, each control has a Sequence attribute that defines where they will render in the group. The default controls are based on multiples of 10, so any custom controls should not use a multiple of 10 to avoid collisions. The Command attribute is used by the CommandUIHandler defined later in this topic but is required even if the CommandUIHandler is not specified. The TemplateAlias attribute defines where the control will render in relationship to the GroupTemplate.
Description="This is a custom group!"
Description="Says Hello to the World!"
Description="Says Good-bye to the World!"
Description="Says I <3 the World!"
LabelText="I <3 you, World!"
With that code in place, you have finished the first CommandUIDefinition that includes the tab, groups, and controls. You need to define how the controls will render inside of the group. You do this using a GroupTemplate element in another CommandUIDefinition. The CommandUIDefinition will have a Location of Ribbon.Templates._children. This follows the pattern for Groups and Tabs.
The GroupTemplate element contains a Layout element with Section and Row elements. The Layout element has a LayoutTitle that is used for the Size attribute on the MaxSize and Scale elements. The Section elements define the position of the controls as well as how many rows will render inside that section. The Row element contains one or more ControlRef elements. ControlRef elements define how your controls will display. The DisplayMode attribute can have the following values:
Renders as a small icon without label text.
Renders as a 16x16 icon with label text.
Renders as a 32x32 icon with label text.
Renders as text only.
The ControlRef elements also define the TemplateAlias values that you used for the TemplateAlias attribute on the buttons. Note that only one control per group can use this value. You cannot have two controls with the same TemplateAlias unless it is an OverflowArea. You can reuse the TemplateAlias across different groups though. There is also an OverflowArea element that defines how controls will render if there are too many controls in a group. The TemplateAlias attributes that refer to OverflowAreas in the GroupTemplate can be used on multiple controls.
In this example, you will define two sections for rendering the controls. One section will be a row of large buttons while the other section will contain two rows of medium-sized buttons.
<Section Alignment="Top" Type="OneRow">
<ControlRef DisplayMode="Large" TemplateAlias="cust1" />
<Section Alignment="Top" Type="TwoRow">
<ControlRef DisplayMode="Medium" TemplateAlias="cust2" />
<ControlRef DisplayMode="Medium" TemplateAlias="cust3" />
With the addition of the CommandUIHandlers, you have finished your Elements.xml file. Press F5 to deploy your solution. Then, navigate to the Shared Documents folder. You should see the Custom Tab. Click on Custom Tab to open it. Click any of the buttons to show the associated alert.
Hopefully this walkthrough has been helpful! If you have any questions or concerns, please let me know in the comments.
Full Code Listing:
Title="My Custom Tab"
Description="This holds my custom commands!"
Description="Says hello to the World!"
Description="Says good-bye to the World!"
Description="Says I love the World!"
LabelText="I love you, World!"