Working With Web Part Properties Of SharePoint Online Pages Using PnP Core CSOM Library

Introduction
 
In this article, you will learn how to retrieve and update the properties of web part available on a page from SharePoint online sites, using PnP Core CSOM library.
 
The main advantage of using PnP Core libraries is the reduced code to get/set the required information. The required object can be retrieved/created/updated with a very small piece of code, once the client context is set.
 
Prerequisite
  • PnP Core CSOM documentation can be found on the official site here.
  • PnP Core CSOM library packages can be downloaded here
The code, given below, is being tested, using Visual Studio console application. Once the console application is created, the packages can be installed, using "Install-Package SharePointPnPCoreOnline" command on Package Manager console of Visual Studio. Once installed, the references and packages will be imported to the solution.
 
The references used in the sample are given below.
  • Microsoft.SharePoint.Client
  • OfficeDevPnP.Core
 Connect to SharePoint Online site
 
The Authentication Manager is used to retrieve the client context of the site. To connect to SharePoint Online site, the token, given below is used.
  • GetSharePointOnlineAuthenticatedContextToken
The parameters required are.
  • SharePoint Online site URL
  • UserId
  • Password (or secured string) 
Retrieve Web Part Properties
 
The properties of web parts can be retrieved from a page. The following steps explain the process in detail.
  • Input the site detail, user details for authentication, page details and web part info.
  • Authenticate and get the client context of the site and then the necessary Web object.
  • Using Web object, retrieve the web part properties using GetWebPartProperties method. The required parameters are given below.

    • Page URL
    • Web Part ID (GUID)

  • Display the results.
The following code shows the operation.
  1. // Input Parameters      
  2. string siteUrl = "https://nakkeerann.sharepoint.com";  
  3. string userName = "abc@abc.onmicrosoft.com";  
  4. string password = "***";  
  5.   
  6. // PnP component to set context      
  7. AuthenticationManager authManager = new AuthenticationManager();  
  8. try  
  9. {  
  10.     // Get and set the client context  
  11.     // Connects to SharePoint online site using inputs provided  
  12.     using (var clientContext = authManager.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))  
  13.     {  
  14.         // Input Parameters  
  15.         string pageUrl = "/Pages/TestPage.aspx";  
  16.         Guid webpartId = new Guid("2c709ca2-17cf-4a47-bc93-93d3b27d4abd");  
  17.         // Retrieves web part properties  
  18.         PropertyValues webpartProps = clientContext.Site.RootWeb.GetWebPartProperties(webpartId, pageUrl);  
  19.   
  20.         // Displays results  
  21.         Console.WriteLine("Title : " + webpartProps["Title"]);  
  22.         Console.ReadKey();  
  23.     }  
  24. }  
  25. catch (Exception ex)  
  26. {  
  27.     Console.WriteLine("Error Message: " + ex.Message);  
  28.     Console.ReadKey();  
  29. }  
The following snapshot shows the properties retrieved on console. Similarly, other properties can also be retrieved.
 
 
 
Update Web Part Properties

The properties of web parts can be updated on a page. The following steps explain the process in detail.
  • Input the site detail, user details for authentication, page details and web part info.
  • Authenticate and get the client context of the site and then the necessary Web object.
  • Using Web object, update the web part properties using SetWebPartProperty method. The required parameters are given below.

    • Property Name
    • Property Value
    • Page URL
    • Web Part ID (GUID)

  • Display the results.
The following code shows the operation.
  1. // Input Parameters      
  2. string siteUrl = "https://nakkeerann.sharepoint.com";  
  3. string userName = "abc@abc.onmicrosoft.com";  
  4. string password = "***";  
  5.   
  6. // PnP component to set context      
  7. AuthenticationManager authManager = new AuthenticationManager();  
  8. try  
  9. {  
  10.     // Get and set the client context  
  11.     // Connects to SharePoint online site using inputs provided  
  12.     using (var clientContext = authManager.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))  
  13.     {  
  14.         // Input Parameters  
  15.         string pageUrl = "/Pages/TestPage.aspx";  
  16.         Guid webpartId = new Guid("2c709ca2-17cf-4a47-bc93-93d3b27d4abd");  
  17.           
  18.         string propertyName = "Title";  
  19.         string propertyValue = "Title updated";  
  20.   
  21.         // Updates the web part properties  
  22.         clientContext.Site.RootWeb.SetWebPartProperty(propertyName, propertyValue, webpartId, pageUrl);  
  23.   
  24.         // Displays the results  
  25.         Console.WriteLine("Updated the properties of a web part on page");  
  26.         Console.ReadKey();  
  27.     }  
  28. }  
  29. catch (Exception ex)  
  30. {  
  31.     Console.WriteLine("Error Message: " + ex.Message);  
  32.     Console.ReadKey();  
  33. }   
The following snapshot shows the updated web part title on a page. Similarly, other properties can also be updated.



Summary
 
Thus you have learned retrieving and updating the web part properties of a SharePoint online page using PnP Core CSOM Library. The operations are compatible for SharePoint online (o365) sites. For on-premise site, the authentication needs to be changed.