top of page

Change Page Layout Using CSOM With PowerShell in SharePoint Online


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.

1. 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.

  1. Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"

  2. Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

  3. Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Publishing.dll"

2. Initialize client context object with the site URL.

  1. $siteURL = ""

  2. $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)  

3. 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. 

  1. # Not required for on premise site - Start

  2. $userId = ""

  3. $pwd = Read-Host -Prompt "Enter password" -AsSecureString  

  4. $creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userId, $pwd)  

  5. $ctx.credentials = $creds   

  6. # Not required for on premise site - End

4. 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.

5. 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.

  1. # page to be changed $pageName = "TestPage1.aspx"

  2. # Get the page to change the layout

  3. $lists = $ctx.web.Lists   

  4. $list = $lists.GetByTitle("Pages")  

  5. $query = New-Object Microsoft.SharePoint.Client.CamlQuery  

  6. $query.ViewXml = "<View><Query><Where><Contains><FieldRef Name='FileLeafRef' /><Value Type='Text'>"+$pageName+"</Value></Contains></Where></Query></View>"

  7. $listItems = $list.GetItems($query)  

  8. $ctx.load($listItems)      

  9. $ctx.executeQuery()  

  10. # Check if page present

  11. if($listItems.Count -ge 1){       

  12. Write-Host "Page exists! Further Code goes here"

  13. }  

6. 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.

  1. # Get the page layout from master page gallery

  2. $pageLayoutList = $ctx.web.Lists.GetByTitle("Master Page Gallery")  

  3. $pageLayoutQuery = New-Object Microsoft.SharePoint.Client.CamlQuery  

  4. $pageLayoutQuery.ViewXml = "<View><Query><Where><Eq><FieldRef Name='FileLeafRef' /><Value Type='Text'>ArticleLeft.aspx</Value></Eq></Where></Query></View>"

  5. $pageLayouts = $pageLayoutList.GetItems($pageLayoutQuery)  

  6. $ctx.Load($pageLayouts)  

  7. $ctx.ExecuteQuery()  

  8. # Retrieve one single layout out of collection

  9. $pageLayout = $pageLayouts[0]  

7. 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.

  1. $page = $listItems[0]  

  2. $file = $page.File  

  3. $ctx.Load($file)  

  4. $ctx.ExecuteQuery()  

  5. # Check if page already checked out. If so, undo check out

  6. if($file.CheckOutType -eq [Microsoft.SharePoint.Client.CheckOutType]::Online){      

  7. Write-Host "Undo Checkout"     

  8. $file.UndoCheckOut()      

  9. $ctx.load($file)      

  10. $ctx.ExecuteQuery()              

  11. }  

  12. # If page is not checked out, then check out

  13. if($file.CheckOutType -eq [Microsoft.SharePoint.Client.CheckOutType]::None){      

  14. Write-Host "Checkout"     

  15. $file.CheckOut()      

  16. $ctx.Load($file)      

  17. $ctx.ExecuteQuery()  

  18. }  

8. Then set Publishing Page layout property of a page with the page layout url and update.

  1. $page["PublishingPageLayout"] = $pageLayout["FileRef"]  

  2. $page.Update()  

9. Check in the file with major version (this will publish the page). Load and execute the query.

  1. $page.File.CheckIn("Layout Modified", [Microsoft.SharePoint.Client.CheckinType]::MajorCheckIn)  

  2. $ctx.load($page)  

  3. $ctx.ExecuteQuery()  

The page will be updated with new page layout.


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.



bottom of page