Retrieve Webparts From Page Using CSOM With PowerShell On SharePoint

Introduction

In this article, you will learn how to get web parts and its properties from a publishing page using CSOM with PowerShell for any SharePoint platform site.

Steps Involved

The following section explains the flow for getting web parts from a publishing page.

  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. Get the page from the respective library using server relative url. Load the file and execute the query to access the page components.
    1. #page to be viewed  
    2. $pageName = "TestPage1.aspx"  
    3. #Get the page  
    4. $file = $ctx.Web.GetFileByServerRelativeUrl("/Pages/TestPage1.aspx")  
    5. $ctx.Load($file)  
    6. $ctx.ExecuteQuery()  
  6. Then from the page, we will get the web parts present. From the file object, using web part manager and personalization scope we can access the page web parts. Load and execute the query to access the web parts.
    1. #Get all the webparts  
    2. Write-Host "Retrieving webparts"  
    3. $wpManager = $file.GetLimitedWebPartManager([Microsoft.SharePoint.Client.WebParts.PersonalizationScope]::Shared)  
    4. $webparts = $wpManager.Webparts  
    5. $ctx.Load($webparts)  
    6. $ctx.ExecuteQuery()  
  7. Access the web parts.

    1. Check the web part count and if web parts present, access the individual web parts using foreach loop.

    2. For each web part, the context should be loaded with web part properties and executed.

    3. Then $webpart.Id will get us web part GUID and $webpart.WebPart.Properties.FieldValues will have all the properties defined for a web part. you can use foreach loop to get the necessary properties.
      1. if($webparts.Count -gt 0){  
      2.     Write-Host "Looping through all webparts"  
      3.     foreach($webpart in $webparts){  
      4.         $ctx.Load($webpart.WebPart.Properties)  
      5.         $ctx.ExecuteQuery()  
      6.         $propValues = $webpart.WebPart.Properties.FieldValues  
      7.         Write-Host "ID: $webpart.id"  
      8.         foreach($property in $propValues){  
      9.             Write-Host "Title: " $property.Title                  
      10.             Write-Host "Description: " $property.Description  
      11.             Write-Host "Chrome Type: " $property.ChromeType  
      12.         }  
      13.               
      14.     }  
      15. }  

Like wise other properties can also be retrieved. The following snapshot shows the properties which can be retrieved for any web part present on the page. This image shows the content editor web part properties present on the page.

.
 
Summary
 
Thus you have learned how to access the web parts and its properties from a publishing page on any SharePoint platform site using CSOM with PowerShell commands.