Subscribe: SharePoint SharePoint SharePoint
Showing posts with label SharePoint 2010 – PowerShell Approve Pages. Show all posts
Showing posts with label SharePoint 2010 – PowerShell Approve Pages. Show all posts

Tuesday, February 4, 2014

Check in and approve all publishing pages using PowerShell in SharePoint

We have a SharePoint 2013 farm setup for our company Intranet. There are huge number of sites in the intranet. Each site is maintained by a separate group. Now the people who maintain those sites are non-technical people.

Since all the sites are publishing sites, so the pages need to be published everytime they are edited. I have repeatedly sent mails to all groups stating that this procedure has to followed everytime they make changes.

But alas! , everyday my mailbox is clogged with requests reporting that "changes are not being reflected to site visitors". Instead of sending separate mails to each of them, i made a powershell script to publish all their pages together.

Note: The image is just for reference. Its not actual screenshot of the below code.

I actually used a timer service to run this script everyday at 5 AM. Finally after few days, i started getting very few requests. So thought of sharing the script:

 $listname = "Pages"   
 $url = "http://myWebApp/sites/mySite"  
    
 function approvePublishContent ($w, $listName) {  
  $list = $w.Lists |? {$_.Title -eq $listName}  
  foreach ($item in $list.Items)   
  {
   //Checking if item is locked
   if(($item -ne $null) -and ($item.LockId -ne $null)) {  
    $item.ReleaseLock($item.LockId)  
   }  
   if( $item.File -ne $null) { $itemFile = $list.GetItemById($item.ID).File }  
   else { $itemFile = $list.GetItemById($item.ID) }  
   
   //Checking in the item
   if( $itemFile.CheckOutStatus -ne "None" ) {   
    $itemFile.CheckIn("Automatic CheckIn. (SysAdmin)")  
    if( $item.File -ne $null) { $itemFile = $list.GetItemById($item.ID).File }  
    else { $itemFile = $list.GetItemById($item.ID) }  
   }  

   //Publishing the item
   if( $list.EnableVersioning -and $list.EnableMinorVersions) {   
    $itemFile.Publish("Automatic Publish. (SysAdmin)")  
    if( $item.File -ne $null) { $itemFile = $list.GetItemById($item.ID).File }  
    else { $itemFile = $list.GetItemById($item.ID) }  
   }  

   //Approving it in case approval is required
   if( $list.EnableModeration ) {   
    $itemFile.Approve("Automatic Approve. (SysAdmin)")   
   }  
  }  
 }  
 $site = Get-SPSite $url  
 foreach ( $web in $site.AllWebs )  
 {  
  approvePublishContent $web $listname  
 }  
 Write-Output "OK"