Friday, 13 May 2016

Change page layout Using CSOM With PowerShell For SharePoint Online / SharePoint 2013

Views

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 / SharePoint 2016 sites.


Steps Involved:


The following section explains 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  
      2. $pageName = "TestPage1.aspx"  
      3. #Get the page to change the layout  
      4. $lists = $ctx.web.Lists   
      5. $list = $lists.GetByTitle("Pages")  
      6. $query = New-Object Microsoft.SharePoint.Client.CamlQuery  
      7. $query.ViewXml = "<View><Query><Where><Contains><FieldRef Name='FileLeafRef' /><Value Type='Text'>"+$pageName+"</Value></Contains></Where></Query></View>"  
      8. $listItems = $list.GetItems($query)  
      9. $ctx.load($listItems)      
      10. $ctx.executeQuery()  
      11.  
      12. #Check if page present  
      13. if($listItems.Count -ge 1){  
      14.       
      15.     Write-Host "Page exists! Further Code goes here"  
      16. }  
  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.  
      6. #Check if page already checked out. If so, undo check out  
      7. if($file.CheckOutType -eq [Microsoft.SharePoint.Client.CheckOutType]::Online){  
      8.     Write-Host "Undo Checkout"  
      9.     $file.UndoCheckOut()  
      10.     $ctx.load($file)  
      11.     $ctx.ExecuteQuery()              
      12. }  
      13.  
      14. #If page is not checked out, then check out  
      15. if($file.CheckOutType -eq [Microsoft.SharePoint.Client.CheckOutType]::None){  
      16.     Write-Host "Checkout"  
      17.     $file.CheckOut()  
      18.     $ctx.Load($file)  
      19.     $ctx.ExecuteQuery()  
      20. }  
  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.