Skip to content

Instantly share code, notes, and snippets.

@asadrefai
Created August 11, 2015 09:40
Show Gist options
  • Save asadrefai/d59cb8d58f1602df2888 to your computer and use it in GitHub Desktop.
Save asadrefai/d59cb8d58f1602df2888 to your computer and use it in GitHub Desktop.
Change page layout of a SharePoint publishing page using CSOM PowerShell
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
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment