SharePoint Ribbon customization for a specific content type

I wanted to utilize the new SharePoint 2010 Ribbon for performing a custom action on items in a custom list.  The list contained items that were of a custom content type (lots of custom work going on).  With some great posts from Chris O’Brien and MSDN, I found all the XML needed to accomplish the task.  I also took advantage of the Custom Tab, Button Groups and Notifications while I was at it.

A couple of things to note when targeting the content type for ribbon customization.  The content type that was used for the CustomAction was created through code, so I didn’t have the ID at design time to add to the Ribbon customization.  I deployed the content type first, looked up the content type ID (this can be done quickly by viewing the properties of the CT and looking at the ID in the URL).  Once I had that ID, I was able to set the CustomAction RegistrationId to the ID and the RegriatrationType to “ContentType.”  Also, I only wanted the button to be active when one or more items were selected.  I modified the EnabledScript function to make that happen.

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <CustomAction
    Id="SharePoint.Ribbon.SohemasTab"
    Location="CommandUI.Ribbon"
  RegistrationId="0x0100ECB394A7B088A843A2421FB7663F957602"
  RegistrationType="ContentType" >
    <CommandUIExtension>
      <CommandUIDefinitions>
        <CommandUIDefinition Location="Ribbon.Tabs._children">
          <Tab Id="SharePoint.Ribbon.SohemasTab" Title="Sohema Commands" Description="Sohema Workflow Commands" Sequence="501">
            <Scaling Id="SharePoint.Ribbon.SohemasTab.Scaling">
              <MaxSize Id="SharePoint.Ribbon.SohemasTab.SohemaActionGroup.MaxSize"
                                     GroupId="SharePoint.Ribbon.SohemasTab.SohemaActionGroup"
                                     Size="OneLarge"/>
              <Scale Id="SharePoint.Ribbon.SohemasTab.SohemaActionGroup.Scaling.SohemasTabScaling"
                                   GroupId="SharePoint.Ribbon.SohemasTab.SohemaActionGroup"
                                   Size="OneLarge" />
            </Scaling>
            <Groups Id="SharePoint.Ribbon.SohemasTab.Groups">
              <Group
                              Id="SharePoint.Ribbon.SohemasTab.SohemaActionGroup"
                              Description="Contains Sohema Action items"
                              Title="Perform Sohema Actions"
                              Sequence="52"
                              Template="Ribbon.Templates.OneLargeExample">
                <Controls Id="SharePoint.Ribbon.SohemasTab.SohemaActionGroup.Controls">
                  <Button Id="Ribbon.Documents.New.SohemaPushToCSVRibbonButton"
                    Alt="Push selected item(s) to CSV"
                    Sequence="95"
                    LabelText="Push Sohema(s) To CSV"
                    Image16by16="/_layouts/images/SubcontractorSohema/ToCSV16x16.png"
                    Image32by32="/_layouts/images/SubcontractorSohema/ToCSV32x32.png"
                    Command="Command.SohemaPushButton"
                    TemplateAlias="PushSohema" />
                </Controls>
              </Group>
            </Groups>
          </Tab>
        </CommandUIDefinition>
        <CommandUIDefinition Location="Ribbon.Templates._children">
          <GroupTemplate Id="Ribbon.Templates.OneLargeExample">
            <Layout Title="OneLarge" LayoutTitle="OneLarge">
              <Section Alignment="Top" Type="OneRow">
                <Row>
                  <ControlRef DisplayMode="Large" TemplateAlias="PushSohema" />
                </Row>
              </Section>
            </Layout>
          </GroupTemplate>
        </CommandUIDefinition>
      </CommandUIDefinitions>
      <CommandUIHandlers>
        <CommandUIHandler
                    Command="Command.SohemaPushButton"
                    CommandAction="javascript:
            var notificationId = SP.UI.Notify.addNotification('Processing Sohemas');"
                    EnabledScript="javascript:function moreThanOneEnabled()
            {
              var items = SP.ListOperation.Selection.getSelectedItems();
              var ci = CountDictionary(items);
              return (ci > 0);
            }
            moreThanOneEnabled(); " />
      </CommandUIHandlers>
    </CommandUIExtension>
  </CustomAction>
</Elements>

  1. #1 by Dee on July 7, 2011 - 12:55 pm

    After a long search, i found only on your blog abt EnabledScript.thanks..

  2. #2 by Dee on July 7, 2011 - 1:28 pm

    My button on the ribbon is always active irrespective of the content type.Should it be done on the enabledscript?I thought registrationtype=contenttype takes care of it.

Leave a Reply to Dee Cancel reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: