Cross-posted from Jason Lee's Blog
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.
The logical place to put this button is on the Documents tab. This is part of the Library Tools contextual tab group – it's contextual because it's only displayed when the context is relevant, i.e. when the user browses to a document library. The Documents tab is selected automatically when the user selects one or more documents in the library's list view web part:
Let's take it one bit at a time for now, and I'll provide a full code listing at the bottom. Firstly, like all declarative ribbon customizations, we start with a CustomAction feature element:
<CustomAction Id="Jason.SP.GSD"
Location="CommandUI.Ribbon"
Sequence="11"
RegistrationType="List"
RegistrationId="101">
The key point of note here is that if you plan to add controls to a contextual tab, you must use the RegistrationType and RegistrationId attributes to target your ribbon customizations to an appropriate list type. If you're deploying controls to a standard ribbon tab, you can get away with omitting these attributes. It didn't initially occur to me that it should be any different in this case – I'm adding controls to the Documents tab, the Documents tab only shows up when I'm looking at a document library, I shouldn't have to worry about scope, right? But no – if you don't set these attributes, your controls simply won't show up on the tab. In this case, a RegistrationType of "List" and a RegistrationId of "101" scopes our ribbon customization to the document library base type.
Next, we define the controls we want to add to the ribbon. This process is identical regardless of whether you're adding to a contextual tab or a regular tab. In this case, we want to add a new group named "Jason's Actions" to the Documents tab. Within this group, we want to create a single button labelled "Get Selection Details". To accomplish this we need to create two
CommandUIDefinition elements – one to define the maximum size of my group element, and one to define the group itself. Creating tabs, groups, and controls has been covered comprehensively elsewhere, so I don't want to go into too much detail – if you're looking for more information in this area,
Chris O'Brien's blog post series is an excellent place to start.
In this case, we'll use the absolute minimum markup required to add a new button in its own group - a Group element to define the group and the controls within it, and a MaxSize element that defines how the group should be rendered on the ribbon. You can specify many more elements if you want – for example you can add a Scale element to specify how your group should render at different sizes, and you can define your own GroupTemplate to specify precisely how controls within your group should be arranged. However, each group must have a matching MaxSize element – otherwise it won't appear on the tab. The easiest approach to creating ribbon controls is to pick out existing controls that resemble what you're looking for and take a look at how they're defined. Let's say we want our group and button to look like the Share & Track group shown here – a large, simple layout with a single control:
To replicate this group, the first step is to take a look at the group definition. Ribbon controls are defined in the 14\TEMPLATE\GLOBAL\XML\CMDUI.XML file. To find specific elements in this file, unless you know the ID of the element you're looking for, it's best to start with the top-level elements and narrow down your search – start by finding the right tab group (Id="Ribbon.LibraryContextualGroup"), then locate the correct tab (Id="Ribbon.Document"), then identify the group you're looking for. In this case, the group we want to borrow from has an ID of "Ribbon.Documents.Share":
<Group Id="Ribbon.Documents.Share"
Sequence="40"
Command="ShareGroup"
Description=""
Title="$Resources:core,cui_GrpShare;"
Image32by32Popup=".../formatmap32x32.png"
Image32by32PopupTop="-128"
Image32by32PopupLeft="-64"
Template="Ribbon.Templates.Flexible2">
By examining the definition of this group, we can figure out the properties we need:
- 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.
Next, we can take a look at how controls are defined within the group. For example, the following markup defines the E-mail a Link button you saw in the previous image:
<Button Id="Ribbon.Documents.Share.EmailItemLink"
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"
/>
In this case, the TemplateAlias attribute is the value that interests us. Every group template contains one or more placeholders, represented by ControlRef elements, in which you can place your controls. In this case, the E-mail a link button specifies that it should be added to the o1 placeholder in the Flexible2 group template. If we use the same value in our own button, we should get the same result.
Finally, we can also take a look at the matching MaxSize element for the Share & Track group. Remember that these elements are always paired – a Group element always has a corresponding MaxSize element defined within the same tab. Within each MaxSize element, the GroupId attribute identifies the corresponding group:
<MaxSize Id="Ribbon.Documents.Scaling.Share.MaxSize"
Sequence="40"
GroupId="Ribbon.Documents.Share"
Size="LargeLarge"
/>
In this case, all we're interested in is the Size attribute. A group template can define multiple layouts, and this attribute identifies the specific layout in the Flexible2 template that we want to use – in this case, the LargeLarge layout.
We can now use all this information we've collected to define our group and button:
<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=".../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>
There are a few additional points worth mentioning at this stage:
- 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".
Note that the button has a Command attribute value of "JL.GetSelectionDetails". This ties the button to a CommandUIHandler element in which we can define the JavaScript that should run when the user clicks the button, as shown below:
<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>
The first point of interest is the
EnabledScript attribute. When you add a control to the Documents tab
it is disabled by default – you must use this attribute to specify the conditions under which the control should be enabled. The
EnabledScript attribute should specify (or call) a JavaScript function that returns a Boolean value –
true to enable the control,
false to disable it. In this case, we want the button to be enabled when the user has selected a single document in the document library list view. The JavaScript client-side object model for SharePoint includes a class named
SP.ListOperation.Selection for just this kind of eventuality. We can use the
getSelectedItems method to return a collection of the items selected in the list view, then check that the
length of the collection is equal to 1.
Note: In this example I've added all my JavaScript logic directly to the CommandUIHandler element. As your JavaScript logic grows larger and more complex, a better option would be to deploy a standalone JavaScript file. Yaroslav Pentarskyy describes this approach in this blog post.
Next, the CommandAction attribute specifies the JavaScript function we want to call when our button is clicked. The getSelectedItems method returns a Dictionary of key-value pairs. The value of each dictionary entry is an object with two attributes – id and fsObjType. The id attribute represents the integer ID of the list item, while the fsObjType attribute represents the type of list item object – 0 for a document or a list item, 1 for a folder. While this doesn't give us a great deal of information about the selected item, the integer ID gives us enough information to submit a query for additional document metadata, should we so wish. In this case, as a proof of concept, we simply display a notification containing the document ID when the user clicks our button.
Here's the button in its default disabled state:
When we select a document, the button is enabled:
When we click the button, a notification displays the integer ID of the selected document:
And that concludes today's task. Next time I plan to cover how to extend this to do something useful with the selected document. The contents of the feature element are shown below in their entirety for reference.
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<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>