Introduction
In this article, you will learn how to change the page layout of a publishing page using CSOM with PowerShell for SharePoint Online / SharePoint 2013.
Steps Involved
The following section explains to you the flow for changing the page layout.
- Add the references using the Add-Type command with necessary reference paths. The necessary references are Microsoft.SharePoint.Client.dll, Microsoft.SharePoint.Client.Runtime.dll and Microsoft.SharePoint.Client.Publishing.dll.
- 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"
- Initialize client context object with the site URL.
- $siteURL = ""
- $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)
- If you are trying to access SharePoint Online site, then you need to setup the site credentials with credentials parameter and get it set to the client context.
- #Not required for on premise site - Start
- $userId = ""
- $pwd = Read-Host -Prompt "Enter password" -AsSecureString
- $creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userId, $pwd)
- $ctx.credentials = $creds
- #Not required for on premise site - End
- If you are trying to access the SharePoint on premise site, then the credentials parameter is not required to be set to the context. But you need to run the code on the respective SharePoint server.
- Then check if the page already exists.
- Get the page library using GetByTitle method from the list collection.
- Then initialize the query object and set the query using view xml method.
- Get the items using GetItems method.
- Load and execute the query.
- Then check the items count.
- #page to be changed
- $pageName = "TestPage1.aspx"
- #Get the page to change the layout
- $lists = $ctx.web.Lists
- $list = $lists.GetByTitle("Pages")
- $query = New-Object Microsoft.SharePoint.Client.CamlQuery
- $query.ViewXml = "<View><Query><Where><Contains><FieldRef Name='FileLeafRef' /><Value Type='Text'>"+$pageName+"</Value></Contains></Where></Query></View>"
- $listItems = $list.GetItems($query)
- $ctx.load($listItems)
- $ctx.executeQuery()
-
- #Check if page present
- if($listItems.Count -ge 1){
-
- Write-Host "Page exists! Further Code goes here"
- }
- If the page is found, we go ahead in changing the layout. Before that, we need to retrieve the page layout details to change. By default page layouts are present in master pages gallery.
- Get the page layout library and filter out page layout item using the query object.
- Once the query is set, get the necessary page layout using GetItems method.
- Since, it returns the item collection, we will take the first item from the collection.
- #Get the page layout from master page gallery
- $pageLayoutList = $ctx.web.Lists.GetByTitle("Master Page Gallery")
- $pageLayoutQuery = New-Object Microsoft.SharePoint.Client.CamlQuery
- $pageLayoutQuery.ViewXml = "<View><Query><Where><Eq><FieldRef Name='FileLeafRef' /><Value Type='Text'>ArticleLeft.aspx</Value></Eq></Where></Query></View>"
- $pageLayouts = $pageLayoutList.GetItems($pageLayoutQuery)
- $ctx.Load($pageLayouts)
- $ctx.ExecuteQuery()
- #Retrieve one single layout out of collection
- $pageLayout = $pageLayouts[0]
- Before changing layout, we need to check out the page.
- Get the file from page object.
- Check if page already checked out by other users. If so, then undo check out for editing.
- If its not checked out, then checkout here.
- $page = $listItems[0]
- $file = $page.File
- $ctx.Load($file)
- $ctx.ExecuteQuery()
-
- #Check if page already checked out. If so, undo check out
- if($file.CheckOutType -eq [Microsoft.SharePoint.Client.CheckOutType]::Online){
- Write-Host "Undo Checkout"
- $file.UndoCheckOut()
- $ctx.load($file)
- $ctx.ExecuteQuery()
- }
-
- #If page is not checked out, then check out
- if($file.CheckOutType -eq [Microsoft.SharePoint.Client.CheckOutType]::None){
- Write-Host "Checkout"
- $file.CheckOut()
- $ctx.Load($file)
- $ctx.ExecuteQuery()
- }
- Then set Publishing Page layout property of a page with the page layout url and update.
- $page["PublishingPageLayout"] = $pageLayout["FileRef"]
- $page.Update()
- Check in the file with major version (this will publish the page). Load and execute the query.
- $page.File.CheckIn("Layout Modified", [Microsoft.SharePoint.Client.CheckinType]::MajorCheckIn)
- $ctx.load($page)
- $ctx.ExecuteQuery()
The page will be updated with new page layout.
Summary
Thus you have learned how to change the page layout of a publishing page on SharePoint Online / SharePoint 2013 sites using CSOM with PowerShell commands.