Tuesday, April 24, 2012

PowerShell Update Publishing Page Properties

Here is the simple powershell script to update the Publishing Pages properties. The web url and list name is read from the user.
#Declare variables

#Read web site url
$Url=Read-Host "Enter a web site url"

#Read list name
$ListName=Read-Host "Enter list name"

$FieldInternalName = "ArticleSortOrder"
$comment = "Added article sort order"

#Get web, list objects
$web = Get-SPWeb -Identity $Url
$spList = $web.Lists[$ListName]
if(!$spList)
{
 write-host $ListName" list doesn't exist"
 $web.Dispose()
 return
}

if ([Microsoft.SharePoint.Publishing.PublishingWeb]::IsPublishingWeb($web)) 
{
 write-host "Reviewing pages in"$web.Title"site...."
 #Get the Publishing Web and pages within it
 $publishingWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)
 $publishingPages = $publishingWeb.GetPublishingPages()
 foreach ($publishingPage in $publishingPages)
 {
  $file = $web.GetFile($publishingPage.Uri.ToString())

  [Microsoft.SharePoint.SPListItem]$spListItem = $file.Item
  $FieldValue = $spListItem[$FieldInternalName]
  if(!$FieldValue -or $FieldValue -eq 0)
  {
   $FieldValue = 9999
   
   Write-Host "Checking out page: " $publishingPage.Title
   $file.CheckOut()
   
   Write-Host "Updating`t" $spListItem.Name "`tArticleSortOrder to $FieldValue"
   $spListItem[$FieldInternalName] = $FieldValue
   $spListItem.Update()

   $file.CheckIn($comment, [Microsoft.SharePoint.SPCheckinType]::MajorCheckIn)        
   write-host "Added article sort order and checked in file: " $publishingPage.Title "`n"
  }
  else
  {   
   continue
  }
 }
}
else
{
    #Notify user that the site is not a publishing site
    write-host $web.Title"is not a publishing site"
}
$web.Dispose()

2 comments:

  1. Awesome script.. suggested improvements :
    1. Should skip or handle checked out pages properly.

    ReplyDelete
  2. Great script man...was looking for something like this since a while..
    Cheers!

    ReplyDelete