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.
# Description # Check-in and Publishes all checked-out pages in a site collection # # Syntax # ./pubPages [-all] # # Parameters # -all # Also check-in and publish subsites in the site collection # # Settings # Only change the -value parameter! # set-variable -option constant -name url -value "http://localhost" # Site collection set-variable -option constant -name comment -value "System Approval" # Publishing comment # End of settings # Function: Approve-PublishingPage # Description: Approve a single page in a Publishing Web # Parameters: publishingPage PublishingPage object # comment Comment to accompany the check-in/approve/publish # function Approve-PublishingPage ([Microsoft.SharePoint.Publishing.PublishingPage]$publishingPage, [string]$comment) { " Publishing Page: " + $publishingPage.Name $listitemfile = $publishingPage.ListItem.File # Check item if checked out if ($listitemfile.Level -eq [Microsoft.SharePoint.SPFileLevel]::Checkout) { " Checking in page" $listitemfile.CheckIn($comment,[Microsoft.SharePoint.SPCheckInType]::MajorCheckin ) } # If moderation is being used then handle the approval and publishing if ($publishingPage.ListItem.ParentList.EnableModeration) { $modInformation = $publishingPage.ListItem.ModerationInformation " Moderation Enabled" # Check for pending approval if($modInformation.Status -eq [Microsoft.SharePoint.SPModerationStatusType]::Pending) { " Approving" $listitemfile.Approve($comment) } # Publish if($modInformation.Status -eq [Microsoft.SharePoint.SPModerationStatusType]::Draft) { " Publishing" $listitemfile.Publish($comment) } } } # Function: Approve-AllPagesInSPWeb # Description: Loop through all the pages in a Publishing Web and checkin and approve them # Parameters: web SPWeb object # comment Comment to accompany the checkin/approve/publish # function Approve-AllPagesInSPWeb([Microsoft.SharePoint.SPWeb]$web, [string]$comment) { # Check this is a publishing web if ([Microsoft.SharePoint.Publishing.PublishingWeb]::IsPublishingWeb($web) -eq $true) { $pubweb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web); "Checking $($pubweb.URL)" $pubcollection=$pubweb.GetPublishingPages() for($i=0; $i -lt $pubcollection.count; $i++) { Approve-PublishingPage $pubcollection[$i] $comment } $web.Dispose() } } $site = new-object Microsoft.SharePoint.SPSite($url) # Handle parameters if($args[0] -eq "-all") { $site.allwebs | foreach {Approve-AllPagesInSPweb $_. $comment} } else { $site.rootweb | foreach {Approve-AllPagesInSPweb $_. $comment} } # Dispose of objects $site.Dispose() # Changelog # # v1.0 - November 27th, 2008 # First public release




