Albert Jan Schot
 

Batch Publish with the Ribbon

17

Aug

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.

Albert-Jan Schot schreef

Comments (0)

Albert-Jan Schot

SharePoint 2007 to 2010 upgrade issue with navigation

03

Aug

Upgrading a SharePoint 2007 to a SharePoint 2010 environment and all options you have is something that is handled quite well on the web, personally i love this post on how to do it, but there are several other posts out there making it pretty easy. However I ran into a ‘small’ bug upgrading.

I have a SharePoint farm containing some info, and a Dutch language pack, thus I recreated the same environment as a SharePoint 2010 containing a Dutch language pack, and using a database upgrade. And as you can see in the picture below, there are no errors, and adding the content DB went well as expected.

addcontentdb 

However, the results when looking at in the browser where kind of sad:

Foutmelding menu

After a long search on the web someone mentioned turning off and on the Publication Infrastructure for SharePoint, turning it of resolved the issue, but turning it on again instantly returned it. So I found myself struggling with the issue for another day not being able to find the exact bug, but nice fix.

Apparently the Menu control expects a /paginas library that contains the pages, but it isn’t there, so it throws an error, since I didn't want to change my farm to much I created a small PowerShell scripts that would fix the error.

The result is a working site:

Werkend menu

Script 1 – Creating Libraries 

  1: [Void][System.Reflection.Assembly]::LoadWithPartialName
  2:   ("Microsoft.SharePoint")
  3: [Void][System.Reflection.Assembly]::LoadWithPartialName
  4:   ("Microsoft.SharePoint.Publishing")
  5: 
  6: function CheckaWeb($rootweb)
  7: {
  8:   #Step 1 Check For Lists
  9:   $list = $rootweb.Lists[$from];
 10:   if($list -ne $null) {createList $rootweb $list}
 11:   
 12:   #Check for subwebs
 13:   if($rootweb.Webs -ne $null)
 14:   {
 15:     # Recursive Call To SubWeb
 16:     foreach($web in $rootweb.Webs)
 17:     {
 18:        CheckAWeb $web
 19:     }
 20:   }
 21: }
 22: 
 23: 
 24: function createList ($web, $list)
 25: {
 26:   Write-Host -ForegroundColor Green 
 27:           " Found a web containing a PageList on url" $web.Url 
 28:   
 29:   $web.Lists.Add("Paginas", 
 30:             "Automatisch gegenereerde bibliotheek voor 
 31:              SharePoint 2010 upgrade", 101)
 32: }
 33: 
 34: 
 35: Write-Host ""
 36: Write-Host -ForegroundColor Green 
 37:     "Fix Dutch SharePoint 2010 Upgrade Script v1.0 - Albert-Jan Schot "
 38: Write-Host -ForegroundColor Green 
 39:     "contact appie@tamtam.nl"
 40: Write-Host ""
 41: 
 42: $site=new-object Microsoft.SharePoint.SPSite('http://restore.sp2010.dev')
 43: $from = "Pagina's"
 44: 
 45: Write-Host -f Blue " "
 46: 
 47: CheckaWeb $site.Rootweb $true  
 48: 
 49: Write-Host " "
 50: Write-Host -f Blue "Done checkin webs"
 51: Write-Host " "

Albert-Jan Schot schreef

Comments (0)

Albert-Jan Schot