Page layouts of a page can also be changed via PowerShell
script. Here is a CSOM PowerShell
function which will change the page layout.
Since its CSOM you need not run this on server, i.e. this can be executed from remote machine provide you have installed SharePoint Client SDK
.
Try{
Add-Type -Path 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll'
Add-Type -Path 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll'
Add-Type -Path 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Publishing.dll'
Add-Type -Path 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Taxonomy.dll'
}
catch {
Throw "Unable to load SharePoint Client runtime"
}
function ChangePageLayout()
{
param(
[Parameter(Mandatory=$true)][string]$siteurl,
[Parameter(Mandatory=$false)][System.Net.NetworkCredential]$credentials,
[Parameter(Mandatory=$false)][string]$PageName,
[Parameter(Mandatory=$false)][string]$PageLayoutName,
[Parameter(Mandatory=$false)][string]$PageLayoutDisplayName,
[Parameter(Mandatory=$false)][string]$Title,
[Parameter(Mandatory=$false)][bool]$isCustomPageLayout
)
try
{
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteurl)
$ctx.Credentials = $credentials
if($isCustomPageLayout -eq $false)
{
$PageLayoutName = "/_catalogs/masterpage/" + $PageLayoutName + "," + $PageLayoutDisplayName
}
else
{
#Here I have assumed that if its custom page layout, then it's placed inside some folder which is child to masterpage
#If that's not the case with you then you can use below line of code
#$PageLayoutName = "/_catalogs/masterpage/" + $PageLayoutName + ", " + $PageLayoutDisplayName
#
$PageLayoutName = "/_catalogs/masterpage/Custom Page Layouts/" + $PageLayoutName + ", " + $PageLayoutDisplayName
}
$Pages = $ctx.Web.Lists.GetByTitle('Pages')
$camlQuery = New-Object Microsoft.SharePoint.Client.CamlQuery
$camlQuery.ViewXml = '<View><Query><Where><Eq><FieldRef Name="FileLeafRef" /><Value Type="Text">'+ $PageName +'</Value></Eq></Where></Query></View>'
$Page = $Pages.GetItems($camlQuery)
$ctx.Load($Page)
$ctx.ExecuteQuery()
$file = $Page.File
$ctx.Load($file)
$ctx.ExecuteQuery()
if ($file.CheckOutType -ne [Microsoft.SharePoint.Client.CheckOutType]::None) {
$file.UndoCheckOut()
$ctx.Load($file)
$ctx.ExecuteQuery()
}
$file.CheckOut()
$ctx.Load($file)
$ctx.ExecuteQuery()
$Page.Set_Item("PublishingPageLayout", $PageLayoutName)
$Page.Set_Item("Title", $Title)
$Page.Update()
$Page.File.CheckIn("", [Microsoft.SharePoint.Client.CheckinType]::MajorCheckIn)
$Page.File.Publish("")
#check for approval
$ctx.Load($Pages)
$ctx.ExecuteQuery()
if ($Pages.EnableModeration -eq $true) {
$Page.File.Approve("")
}
$ctx.Load($Page)
$ctx.ExecuteQuery()
Write-Host "Update Page Layout Complete"
Write-Host ""
}
catch
{
Write-Host ("Error while changing page layout. Error -->> " + $_.Exception.Message) -ForegroundColor Red
}
}
Usage:
$credentials = Get-Credential
ChangePageLayout "http:yoursite.com" $credentials "default.aspx" "YourCustomLayout.aspx" "Your Custom Page Layout" "Some Title" $true