How to Set the Page Layout in SharePoint 2013 using PowerShell

Sathish Nadarajan
 
Solution Architect
December 5, 2015
 
Rate this article
 
Views
11770

In this article, let us see how to change the PageLayout of a Publishing page using PowerShell in SharePoint 2013. All the PowerShell Scripts will be very much helpful and handy during the time of preparing the deployment package.

The Script is straight forward and does not require any explanation I guess.

 ##================================================================================================
 ## Description	: To do the below two items.
     #1. Update the PageLayouts of the Existing Pages
 ## Author		: Sathish Nadarajan
 ## Date			: 16-Nov-2015
 ##================================================================================================
 
 ##============================================ Setup Input Paths ===========================================================
 
 cls
  
 $Host.UI.RawUI.WindowTitle = "-- Update the Page Layouts of the Existing Pages --"
 
 $StartDate = Get-Date
 Write-Host -ForegroundColor White "------------------------------------"
 Write-Host -ForegroundColor White "| Update the Page Layouts of the Existing Pages |"
 Write-Host -ForegroundColor White "| Started on: $StartDate |"
 Write-Host -ForegroundColor White "------------------------------------"
 
 $LogTime = Get-Date -Format yyyy-MM-dd_hh-mm
 $LogFile = ".UpdatePageLayout-$LogTime.rtf"
 
 #start-transcript $logfile
 
 $scriptBase = split-path $SCRIPT:MyInvocation.MyCommand.Path -parent
 Set-Location $scriptBase
 
 $ErrorActionPreference = "Stop"
 
 function AddPowerShellSnapin()
 {
     try
     {
         Write-Host "Adding PowerShell Snap-in" -ForegroundColor Green
         # Try to get the PowerShell Snappin.  If not, then adding the PowerShell snappin on the Catch Block
         Get-PSSnapin "Microsoft.SharePoint.PowerShell" 
     }
     catch
     {
         if($Error[0].Exception.Message.Contains("No Windows PowerShell snap-ins matching the pattern 'Microsoft.SharePoint.PowerShell' were found"))
         {
             Add-PSSnapin "Microsoft.SharePoint.PowerShell"
         }
     }
     Write-Host "Finished Adding PowerShell Snap-in" -ForegroundColor Green
 }
 
 
 function UpdatePageLayout([string]$WebURL, [string]$PageLayoutName,[string]$PublishingPageName)
 {
     #Get the web and page
     $Web = Get-SPWeb $WebURL
     
     #Get Publishing Site and Web
     $PublishingSite = New-Object Microsoft.SharePoint.Publishing.PublishingSite($Web.Site)
     $PublishingWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)
  
     #Get New Page Layout
     $SitePageLayouts = $PublishingSite.GetPageLayouts($false)
     $NewPageLayout = $SitePageLayouts | ? {$_.Name -eq $PageLayoutName}
 
     #Get Pages Library
     $PublishingPage = $PublishingWeb.GetPublishingPage($WebURL + "Pages/" + $PublishingPageName)  
     $PublishingPage.CheckOut()
     $PublishingPage.Layout = $NewPageLayout
     $PublishingPage.ListItem.Update();
     $PublishingPage.CheckIn("Page layout Updated via PowerShell")
     $PublishingPage.ListItem.File.Publish("Published via PowerShell")
     if ($PublishingPage.ListItem.ParentList.EnableModeration)
     {
         $PublishingPage.ListItem.File.Approve("Publishing Page Layout Updated!");
     }
  
     write-host "Updated Page layout on: "$PublishingPage.url -ForegroundColor Green
 }
  
 
  
 
  
  
  try
 {
     [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Sharepoint")
     [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Sharepoint.Administration")
         
     $ConfigXmlPath = $scriptBase + "ConfigXMLPageLayouts.Config.xml"
     Write-Host "Read the Config Values" -ForegroundColor Green 
     [Xml]$Config = Get-Content $ConfigXmlPath  
     
     AddPowerShellSnapin
     
     foreach($Page in $Config.Configuration.Pages.Page)     
     {
         UpdatePageLayout $Config.Configuration.PublishingSite.URL $Page.LayoutName $Page.Name
     }
 }
 catch
 {
     Write-Host "Custom Exception Happened on Main : " + $Error[0].Exception.Message -ForegroundColor Red  
 }
 

And the Config XML is as follows.

 <Configuration EnvironmentName="Dev">
   <PublishingSite URL="http://MySiteCollectionURL/" />
   <Pages>
 	<Page Name="home.aspx" LayoutName=" Home-Layout.aspx"></Page>
 	<Page Name="books.aspx" LayoutName="Topic-Layout.aspx"></Page>
   </Pages>
 </Configuration> 
 
 

 

 

Happy Coding,

Sathish Nadarajan.

Category : PowerShell, SharePoint

Author Info

Sathish Nadarajan
 
Solution Architect
 
Rate this article
 
Sathish is a Microsoft MVP for SharePoint (Office Servers and Services) having 15+ years of experience in Microsoft Technologies. He holds a Masters Degree in Computer Aided Design and Business ...read more
 

Leave a comment