This week I've been taking a look at using ribbon controls with the SharePoint JavaScript client object model to drive some custom functionality. Ribbon customizations for SharePoint 2010 are fairly well documented. However, when you work with contextual tab groups—and the Documents tab in particular—there are a few nuances and idiosyncrasies that it's worth being aware of up front.
In this case, I want to add a ribbon button that enables the user to perform some additional actions when they select a file in a document library. There are countless scenarios in which you might want to do this – for example, you might add a "Request a copy of this document in large print/audio format/Welsh" control to the ribbon and use the document metadata to prepopulate an InfoPath form. To start with, however, I want to keep it simple:
- When the user selects a document in a document library, display a button on the ribbon.
- When the user clicks the button, display some information about the selected document as a client-side notification.
Location="CommandUI.Ribbon"
Sequence="11"
RegistrationType="List"
RegistrationId="101">
Sequence="40"
Command="ShareGroup"
Description=""
Title="$Resources:core,cui_GrpShare;"
Image32by32Popup=".../formatmap32x32.png"
Image32by32PopupTop="-128"
Image32by32PopupLeft="-64"
Template="Ribbon.Templates.Flexible2">
- The Share & Track group has a Template attribute of Ribbon.Templates.Flexible2. This identifies the group template that gets applied to the group (also defined in CMDUI.XML if you want to take a closer look). We'll use this value to apply the same layout to our own group.
- The Share & Track group has a Sequence attribute of 40. We'll use a value of 41 to place our group immediately to the right of the Share & Track group.
Sequence="10"
Command="EmailLink"
Image16by16=".../formatmap16x16.png"
Image16by16Top="-16"
Image16by16Left="-88"
Image32by32=".../formatmap32x32.png"
Image32by32Top="-128"
Image32by32Left="-448"
LabelText="$Resources:core,cui_ButEmailLink;"
ToolTipTitle="$Resources:core,cui_ButEmailLink;"
ToolTipDescription="...,cui_STT_ButEmailLinkDocument;"
TemplateAlias="o1"
/>
Sequence="40"
GroupId="Ribbon.Documents.Share"
Size="LargeLarge"
/>
<CommandUIDefinitions>
<CommandUIDefinition Location="Ribbon.Documents.Scaling._children">
<MaxSize Id="Jason.SP.GSD.JasonsActions.MaxSize"
Sequence="11"
GroupId="Jason.SP.GSD.JasonsActions"
Size="LargeLarge" />
</CommandUIDefinition>
<CommandUIDefinition Location="Ribbon.Documents.Groups._children">
<Group Id="Jason.SP.GSD.JasonsActions"
Sequence="41"
Title="Jason's Actions"
Description="Contains custom document actions"
Template="Ribbon.Templates.Flexible2">
<Controls Id="Jason.SP.GSD.JasonsActions.Controls">
<Button Id="Jason.SP.GSD.JasonsActions.GetButton"
Sequence="1"
Image32by32=".../ThumbsUp.PNG"
LabelText="Get Selection Details"
Description="Gets the details of the selected document"
TemplateAlias="o1"
Command="Jason.SP.GSD.GetCmd" />
</Controls>
</Group>
</CommandUIDefinition>
</CommandUIDefinitions>
- You need a CommandUIDefinition element for each block of XML you want to add to the ribbon.
- When setting the Location attribute, imagine you're slotting the XML directly into the CMDUI.XML file. Look up the ID of the parent element you want to add to, and append "._children" to get your Location value. For example, we want to add our group to the Groups element with an ID of "Ribbon.Documents.Groups", so our Location attribute is "Ribbon.Document.Groups._children".
<CommandUIHandler
Command="Jason.SP.GSD.GetCmd"
EnabledScript="javascript:
SP.ListOperation.Selection.getSelectedItems().length == 1;"
CommandAction="javascript:
var selectedItems =
SP.ListOperation.Selection.getSelectedItems();
var item = selectedItems[0];
var itemID = item['id'];
if (item['fsObjType'] == 0) {
SP.UI.Notify.addNotification(String.format(
'Document selected: ID={0}', itemID));
}
else {
SP.UI.Notify.addNotification(String.format(
'Folder selected: ID={0}', itemID));
}"
/>
</CommandUIHandlers>
</CommandUIExtension>
</CustomAction>
<CustomAction Id="Jason.SP.GSD"
Location="CommandUI.Ribbon"
Sequence="11"
RegistrationType="List"
RegistrationId="101">
<CommandUIExtension>
<CommandUIDefinitions>
<CommandUIDefinition Location="Ribbon.Documents.Scaling._children">
<MaxSize Id=" Jason.SP.GSD.JasonsActions.MaxSize"
Sequence="11"
GroupId="Jason.SP.GSD.JasonsActions"
Size="LargeLarge" />
</CommandUIDefinition>
<CommandUIDefinition Location="Ribbon.Documents.Groups._children">
<Group Id=" Jason.SP.GSD.JasonsActions"
Sequence="41"
Title="Jason's Actions"
Description="Contains custom document actions"
Template="Ribbon.Templates.Flexible2">
<Controls Id="Jason.SP.GSD.JasonsActions.Controls">
<Button Id=" Jason.SP.GSD.JasonsActions.GetButton"
Sequence="1"
Image32by32="/SiteCollectionImages/RibbonIcons/ThumbsUp.PNG"
LabelText="Get Selection Details"
Description="Gets the details of the selected document"
TemplateAlias="o1"
Command=" Jason.SP.GSD.GetCmd" />
</Controls>
</Group>
</CommandUIDefinition>
</CommandUIDefinitions>
<CommandUIHandlers>
<CommandUIHandler Command="Jason.SP.GSD.GetCmd"
EnabledScript="javascript:
SP.ListOperation.Selection.getSelectedItems().length == 1;"
CommandAction="javascript:
var selectedItems =
SP.ListOperation.Selection.getSelectedItems();
var item = selectedItems[0];
var itemID = item['id'];
if (item['fsObjType'] == 0) {
SP.UI.Notify.addNotification(String.format(
'Document selected: ID={0}', itemID));
}
else {
SP.UI.Notify.addNotification(String.format(
'Folder selected: ID={0}', itemID));
}
"/>
</CommandUIHandlers>
</CommandUIExtension>
</CustomAction>
</Elements>