Batch Publish with the Ribbon

Batch Publish with the Ribbon header image

After creating my first ribbon control (blogged about that in this post), I wanted to learn a bit more about the client object model in SharePoint 2010. So due to that I decided to make a new ribbon button that would allow me to publish multiple items, using the Client Object model (allowing you to use the solution as a sandboxed solution. I used almost the same code as in this post, only adding two new things; a custom action containing the script that would actually publish items, and some code so the button would only be enabled whenever there are one or more items selected:

<CustomAction Id="Ribbon.Documents.Custom.Scripts" Location ="ScriptLink" ScriptSrc="/_layouts/Portal/Portal.RibbonActions.js"

and:

<CommandUIHandler
  Command="PublishForMail_PublishButton"
  CommandAction="javascript:PublishDocuments();"
  EnabledScript="javascript:function oneOrMoreEnable() {
   var items = SP.ListOperation.Selection.getSelectedItems();
   var ci = CountDictionary(items);
     return (ci > 0);
  }
  oneOrMoreEnable();" />

So the complete code for the ribbon looked like:

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <CustomAction
    Id="Ribbon.Documents.Custom.BatchPublish"
    Location="CommandUI.Ribbon"
    RegistrationType="ContentType"
    RegistrationId="0x01">
    <CommandUIExtension>
      <CommandUIDefinitions>
        <CommandUIDefinition
          Location="Ribbon.Documents.Groups._children">
          <Group
            Id="Ribbon.Documents.Custom"
            Sequence="100"
            Description="Custom Controls"
            Title="Custom Controls"
            Template="Ribbon.Documents.Custom.CustomTemplate">
            <Controls Id="Ribbon.Documents.Custom.Controls">
              <Button
                Id="Ribbon.Documents.Custom.Controls.BatchPublish"
                Sequence="5"
                Command="BatchPublish_PublishButton"
                Image16by16="/_layouts/images/Portal/publish16.png"
                Image32by32="/_layouts/images/Portal/publish32.png"
                ToolTipTitle="Release Selected Documents"
                ToolTipDescription="Publish a major version of all selected documents"
                LabelText="Release Selection"
                TemplateAlias="o1" />
            </Controls>
          </Group>
        </CommandUIDefinition>
        <CommandUIDefinition
            Location="Ribbon.Templates._children">
          <GroupTemplate Id="Ribbon.Documents.Custom.CustomTemplate">
            <Layout Title="LargeLarge">
              <OverflowSection Type="OneRow" TemplateAlias="o1" DisplayMode="Large"/>
              <OverflowSection Type="OneRow" TemplateAlias="o2" DisplayMode="Large"/>
            </Layout>
            <Layout Title="LargeMedium">
              <OverflowSection Type="OneRow" TemplateAlias="o1" DisplayMode="Large"/>
              <OverflowSection Type="ThreeRow" TemplateAlias="o2" DisplayMode="Medium"/>
            </Layout>
            <Layout Title="LargeSmall">
              <OverflowSection Type="OneRow" TemplateAlias="o1" DisplayMode="Large"/>
              <OverflowSection Type="ThreeRow" TemplateAlias="o2" DisplayMode="Small"/>
            </Layout>
            <Layout Title="MediumLarge">
              <OverflowSection Type="ThreeRow" TemplateAlias="o1" DisplayMode="Medium"/>
              <OverflowSection Type="OneRow" TemplateAlias="o2" DisplayMode="Large"/>
            </Layout>
            <Layout Title="MediumMedium">
              <OverflowSection Type="ThreeRow" TemplateAlias="o1" DisplayMode="Medium"/>
              <OverflowSection Type="ThreeRow" TemplateAlias="o2" DisplayMode="Medium"/>
            </Layout>
            <Layout Title="MediumSmall">
              <OverflowSection Type="ThreeRow" TemplateAlias="o1" DisplayMode="Medium"/>
              <OverflowSection Type="ThreeRow" TemplateAlias="o2" DisplayMode="Small"/>
            </Layout>
            <Layout Title="SmallLarge">
              <OverflowSection Type="ThreeRow" TemplateAlias="o1" DisplayMode="Small"/>
              <OverflowSection Type="OneRow" TemplateAlias="o2" DisplayMode="Large"/>
            </Layout>
            <Layout Title="SmallMedium">
              <OverflowSection Type="ThreeRow" TemplateAlias="o1" DisplayMode="Small"/>
              <OverflowSection Type="ThreeRow" TemplateAlias="o2" DisplayMode="Medium"/>
            </Layout>
            <Layout Title="SmallSmall">
              <OverflowSection Type="ThreeRow" TemplateAlias="o1" DisplayMode="Small"/>
              <OverflowSection Type="ThreeRow" TemplateAlias="o2" DisplayMode="Small"/>
            </Layout>
            <Layout Title="Popup" LayoutTitle="LargeLarge" />
          </GroupTemplate>
        </CommandUIDefinition>
        <CommandUIDefinition
            Location="Ribbon.Documents.Scaling._children">
          <MaxSize
            Id="Ribbon.Documents.Scaling.Custom.MaxSize"
            Sequence="15"
            GroupId="Ribbon.Documents.Custom"
            Size="LargeLarge" />
        </CommandUIDefinition>
      </CommandUIDefinitions>
      <CommandUIHandlers>
        <CommandUIHandler
           Command="BatchPublish_PublishButton"
           CommandAction="javascript:PublishDocuments();"
           EnabledScript="javascript:function oneOrMoreEnable() {
                          var items = SP.ListOperation.Selection.getSelectedItems();
                          var ci = CountDictionary(items);
                            return (ci> 0);
                          }
                          oneOrMoreEnable();" />
      </CommandUIHandlers>
    </CommandUIExtension>
  </CustomAction>
  <CustomAction Id="Ribbon.Documents.Custom.Scripts"
      Location ="ScriptLink"
      ScriptSrc="/_layouts/Portal/Portal.RibbonActions.js" />
</Elements>

And as you could see we would call a function PublishDocuments(), so withing the Portal.RibbonActions.js we would need to have that function. From the context we can get the selected items, and the GUID of the current list, that allows you to create a SPList retrieving the SPListItems based on their ID and do the things you want, finally allowing you to check if actions succeeded and if not make a mention of it.

PublishDocuments = function () {
    var selecteditems = SP.ListOperation.Selection.getSelectedItems();
    var currentListGuid = SP.ListOperation.Selection.getSelectedList();
    var context = SP.ClientContext.get_current();
    var currentWeb = context.get_web();
    var currentList = currentWeb.get_lists().getById(currentListGuid)
    var k;
    for (k in selecteditems) {
        var listitem = currentList.getItemById(selecteditems[k].id);
        var spfile = listitem.get_file();
        spfile.publish("Publish,so file can be send to client");
    }
    context.executeQueryAsync(Function.createDelegate(this, this.success), Function.createDelegate(this, this.failed));
}
function success() {
    SP.UI.Notify.addNotification('Succesfully published major versions');
}
function failed(sender, args) {
    var statusId = SP.UI.Status.addStatus(args.get_message());
    SP.UI.Status.setStatusPriColor(statusId, 'red');
    latestId = statusId;
}

As you can see we try a spfile.publish(), and then check if it was a success, and use the Notify option to show to a user that it was a success, if it failed we use the Status option that shows a red bar with the error.

As you can see with around 30 lines of JavaScript you can batch publish your files.

Loading comments…