How to Set the Page Layout in SharePoint 2013 using PowerShell


Sathish Nadarajan
SharePoint MVP
Published On :   05 Dec 2015
Visit Count
Today :  2    Total :   6942
Plan, Migrate, Secure, Report
SharePoint & Office 365 Tool. Simple & Easy to Use. 15-Day Trial!

Sharegate: Kick-Ass Tool
Think Your SharePoint & Office 365 Are Secure ? Find Out Now!


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 + "\ConfigXML\PageLayouts.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.

SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Categories

Migratiin Tools for SharePoint