Batch Publish with the Ribbon

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:

  1:   <CustomAction Id="Ribbon.Documents.Custom.Scripts" 
  2:       Location ="ScriptLink" 
  3:       ScriptSrc="/_layouts/Portal/Portal.RibbonActions.js"
/>

 

and: 

  1: <CommandUIHandler
  2:  Command="PublishForMail_PublishButton" 
  3:  CommandAction="javascript:PublishDocuments();" 
  4:  EnabledScript="javascript:function
oneOrMoreEnable() { 
  5:   var items
= SP.ListOperation.Selection.getSelectedItems(); 
  6:   var ci
= CountDictionary(items); 
  7:   return (ci
> 0); 
  8:  } 
  9:  oneOrMoreEnable();" />

 

So the complete code for the ribbon looked like:

  1: <?xml version="1.0"
encoding="utf-8"?> 
  2: <Elements xmlns="http://schemas.microsoft.com/sharepoint/"> 
  3:   <CustomAction
  4:     Id="Ribbon.Documents.Custom.BatchPublish" 
  5:     Location="CommandUI.Ribbon" 
  6:     RegistrationType="ContentType" 
  7:     RegistrationId="0x01"> 
  8:     <CommandUIExtension>
  9:       <CommandUIDefinitions>
 10:         <CommandUIDefinition
 11:           Location="Ribbon.Documents.Groups._children"> 
 12:           <Group
 13:             Id="Ribbon.Documents.Custom" 
 14:             Sequence="100" 
 15:             Description="Custom
Controls" 
 16:             Title="Custom
Controls" 
 17:             Template="Ribbon.Documents.Custom.CustomTemplate"> 
 18:             <Controls Id="Ribbon.Documents.Custom.Controls"> 
 19:               <Button
 20:                 Id="Ribbon.Documents.Custom.Controls.BatchPublish" 
 21:                 Sequence="5" 
 22:                 Command="BatchPublish_PublishButton" 
 23:                 Image16by16="/_layouts/images/Portal/publish16.png" 
 24:                 Image32by32="/_layouts/images/Portal/publish32.png" 
 25:                 ToolTipTitle="Release
Selected Documents" 
 26:                 ToolTipDescription="Publish
a major version of all selected documents" 
 27:                 LabelText="Release
Selection" 
 28:                 TemplateAlias="o1"
/> 
 29:             </Controls>
 30:           </Group>
 31:         </CommandUIDefinition>
 32:         
 33:         <CommandUIDefinition
 34:             Location="Ribbon.Templates._children"> 
 35:           <GroupTemplate Id="Ribbon.Documents.Custom.CustomTemplate"> 
 36:             <Layout Title="LargeLarge"> 
 37:               <OverflowSection Type="OneRow"
TemplateAlias="o1" DisplayMode="Large"/> 
 38:               <OverflowSection Type="OneRow"
TemplateAlias="o2" DisplayMode="Large"/> 
 39:             </Layout>
 40:             <Layout Title="LargeMedium"> 
 41:               <OverflowSection Type="OneRow"
TemplateAlias="o1" DisplayMode="Large"/> 
 42:               <OverflowSection Type="ThreeRow"
TemplateAlias="o2" DisplayMode="Medium"/> 
 43:             </Layout>
 44:             <Layout Title="LargeSmall"> 
 45:               <OverflowSection Type="OneRow"
TemplateAlias="o1" DisplayMode="Large"/> 
 46:               <OverflowSection Type="ThreeRow"
TemplateAlias="o2" DisplayMode="Small"/> 
 47:             </Layout>
 48:             <Layout Title="MediumLarge"> 
 49:               <OverflowSection Type="ThreeRow"
TemplateAlias="o1" DisplayMode="Medium"/> 
 50:               <OverflowSection Type="OneRow"
TemplateAlias="o2" DisplayMode="Large"/> 
 51:             </Layout>
 52:             <Layout Title="MediumMedium"> 
 53:               <OverflowSection Type="ThreeRow"
TemplateAlias="o1" DisplayMode="Medium"/> 
 54:               <OverflowSection Type="ThreeRow"
TemplateAlias="o2" DisplayMode="Medium"/> 
 55:             </Layout>
 56:             <Layout Title="MediumSmall"> 
 57:               <OverflowSection Type="ThreeRow"
TemplateAlias="o1" DisplayMode="Medium"/> 
 58:               <OverflowSection Type="ThreeRow"
TemplateAlias="o2" DisplayMode="Small"/> 
 59:             </Layout>
 60:             <Layout Title="SmallLarge"> 
 61:               <OverflowSection Type="ThreeRow"
TemplateAlias="o1" DisplayMode="Small"/> 
 62:               <OverflowSection Type="OneRow"
TemplateAlias="o2" DisplayMode="Large"/> 
 63:             </Layout>
 64:             <Layout Title="SmallMedium"> 
 65:               <OverflowSection Type="ThreeRow"
TemplateAlias="o1" DisplayMode="Small"/> 
 66:               <OverflowSection Type="ThreeRow"
TemplateAlias="o2" DisplayMode="Medium"/> 
 67:             </Layout>
 68:             <Layout Title="SmallSmall"> 
 69:               <OverflowSection Type="ThreeRow"
TemplateAlias="o1" DisplayMode="Small"/> 
 70:               <OverflowSection Type="ThreeRow"
TemplateAlias="o2" DisplayMode="Small"/> 
 71:             </Layout>
 72:             <Layout Title="Popup"
LayoutTitle="LargeLarge" /> 
 73:           </GroupTemplate>
 74:         </CommandUIDefinition>
 75: 
 76:         <CommandUIDefinition
 77:             Location="Ribbon.Documents.Scaling._children"> 
 78:           <MaxSize
 79:             Id="Ribbon.Documents.Scaling.Custom.MaxSize" 
 80:             Sequence="15" 
 81:             GroupId="Ribbon.Documents.Custom" 
 82:             Size="LargeLarge"
/> 
 83:         </CommandUIDefinition>
 84:       </CommandUIDefinitions>
 85:       
 86:       <CommandUIHandlers>
 87:         
 88:         <CommandUIHandler
 89:            Command="BatchPublish_PublishButton" 
 90:            CommandAction="javascript:PublishDocuments();" 
 91:            EnabledScript="javascript:function
oneOrMoreEnable() { 
 92:                           var items
= SP.ListOperation.Selection.getSelectedItems(); 
 93:                           var ci
= CountDictionary(items); 
 94:                             return (ci
> 0); 
 95:                           } 
 96:                           oneOrMoreEnable();" /> 
 97:       </CommandUIHandlers>
 98:     </CommandUIExtension>
 99:   </CustomAction>
100: 
101:   <CustomAction Id="Ribbon.Documents.Custom.Scripts" 
102:       Location ="ScriptLink" 
103:       ScriptSrc="/_layouts/Portal/Portal.RibbonActions.js"
/> 
104: 
105: </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.

  1: PublishDocuments = function ()
{ 
  2:     var selecteditems
= SP.ListOperation.Selection.getSelectedItems(); 
  3:     var currentListGuid
= SP.ListOperation.Selection.getSelectedList(); 
  4: 
  5:     var context
= SP.ClientContext.get_current(); 
  6:     var currentWeb
= context.get_web(); 
  7:     var currentList
= currentWeb.get_lists().getById(currentListGuid) 
  8: 
  9:     var k; 
 10: 
 11:     for (k in selecteditems)
{ 
 12:         var listitem
= currentList.getItemById(selecteditems[k].id); 
 13:         var spfile
= listitem.get_file(); 
 14: 
 15:         spfile.publish("Publish,
so file can be send to client"); 
 16:     }
 17:     
 18:     context.executeQueryAsync(Function.createDelegate(this, this.success), Function.createDelegate(this, this.failed)); 
 19: }
 20: 
 21: function success()
{ 
 22:     SP.UI.Notify.addNotification('Succesfully published major versions'); 
 23: }
 24: function failed(sender,
args) { 
 25:     var statusId
= SP.UI.Status.addStatus(args.get_message()); 
 26:     SP.UI.Status.setStatusPriColor(statusId, 'red'); 
 27:     latestId = statusId;
 28: }

 

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.

Leave a Reply