Aarebrot.net

Frodes website about Sharepoint and other stuff...

  • Increase font size
  • Default font size
  • Decrease font size

Approving multiple pages using Powershell

E-mail Print
User Rating: / 0
PoorBest 

Sometimes I do a bunch of changes to a bunch of pages, without approving them. Then I need to login as a regular user to test something, and I realize I have a dozen pages that aren't visible because they're not approved. Instead of going to each of these pages and approve them all manually, I wrote this script a while back.

This is actually a variation of one of my other scripts, and is based on a script by Colin Byrne. Actually it's almost an exact copy of his script, I just added some extra jazz to it. So if you want to thank someone for this one, thank him. Not me.

  1. # Description
  2. # Check-in and Publishes all checked-out pages in a site collection
  3. #
  4. # Syntax
  5. # ./pubPages [-all]
  6. #
  7. # Parameters
  8. # -all
  9. #  Also check-in and publish subsites in the site collection
  10. #
  11. # Settings
  12. # Only change the -value parameter!
  13. #
  14. set-variable -option constant -name url  -value "http://localhost"   # Site collection
  15. set-variable -option constant -name comment -value "System Approval"   # Publishing comment
  16. # End of settings
  17.  
  18. # Function: Approve-PublishingPage
  19. # Description: Approve a single page in a Publishing Web
  20. # Parameters: publishingPage PublishingPage object
  21. # comment Comment to accompany the check-in/approve/publish
  22. #
  23. function Approve-PublishingPage ([Microsoft.SharePoint.Publishing.PublishingPage]$publishingPage, [string]$comment)
  24. {
  25.  " Publishing Page: " + $publishingPage.Name
  26.  $listitemfile = $publishingPage.ListItem.File
  27.  
  28.  # Check item if checked out
  29.  if ($listitemfile.Level -eq [Microsoft.SharePoint.SPFileLevel]::Checkout)
  30.  {
  31.   " Checking in page"
  32.   $listitemfile.CheckIn($comment,[Microsoft.SharePoint.SPCheckInType]::MajorCheckin )
  33.  }
  34.  
  35.  # If moderation is being used then handle the approval and publishing
  36.  if ($publishingPage.ListItem.ParentList.EnableModeration)
  37.  {
  38.   $modInformation = $publishingPage.ListItem.ModerationInformation
  39.   " Moderation Enabled"
  40.   
  41.   # Check for pending approval
  42.   if($modInformation.Status -eq [Microsoft.SharePoint.SPModerationStatusType]::Pending)
  43.   {
  44.    " Approving"  
  45.    $listitemfile.Approve($comment)
  46.   }
  47.   
  48.   # Publish
  49.   if($modInformation.Status -eq [Microsoft.SharePoint.SPModerationStatusType]::Draft)
  50.   {
  51.    " Publishing"
  52.    $listitemfile.Publish($comment)
  53.   }
  54.  }
  55. }
  56.  
  57. # Function: Approve-AllPagesInSPWeb
  58. # Description: Loop through all the pages in a Publishing Web and checkin and approve them
  59. # Parameters: web SPWeb object
  60. # comment Comment to accompany the checkin/approve/publish
  61. #
  62. function Approve-AllPagesInSPWeb([Microsoft.SharePoint.SPWeb]$web, [string]$comment)
  63. {
  64.  # Check this is a publishing web
  65.  if ([Microsoft.SharePoint.Publishing.PublishingWeb]::IsPublishingWeb($web) -eq $true)
  66.  {
  67.   $pubweb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)
  68.   "Checking $($pubweb.URL)"  
  69.   $pubcollection=$pubweb.GetPublishingPages()  
  70.   for($i=0; $i -lt $pubcollection.count; $i++)
  71.   {
  72.    Approve-PublishingPage $pubcollection[$i] $comment
  73.   }  
  74.   $web.Dispose()
  75.  }
  76. }
  77.  
  78. $site = new-object Microsoft.SharePoint.SPSite($url)
  79.  
  80. # Handle parameters
  81. if($args[0] -eq "-all")
  82. {
  83.  $site.allwebs | foreach {Approve-AllPagesInSPweb $_. $comment}
  84. }
  85. else
  86. {
  87.  $site.rootweb | foreach {Approve-AllPagesInSPweb $_. $comment}
  88. }
  89.  
  90. # Dispose of objects
  91. $site.Dispose()
  92.  
  93. # Changelog
  94. #
  95. # v1.0 - November 27th, 2008
  96. #  First public release

Attachments:
 pubPages.ps1[Powershell: Approve multiple pages]2 KbFeb-11-2010 6:17pm
Last Updated on Thursday, 18 December 2008 11:01  

Add your comment

Your name:
Subject:
Comment:
  The word for verification. Lowercase letters only with no spaces.
Word verification: