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.

  1. Page URL

  2. Web Part ID (GUID)

  • Display the results.

The following code shows the operation. // Input Parameters    

string siteUrl = "https://nakkeerann.sharepoint.com";  

string userName = "abc@abc.onmicrosoft.com";  

string password = "***";  

// PnP component to set context     AuthenticationManager authManager = new AuthenticationManager();  

try

{  

// Get and set the client context

// Connects to SharePoint online site using inputs provided using (var clientContext = authManager.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))      

{  

// Input Parameters

string pageUrl = "/Pages/TestPage.aspx";          

Guid webpartId = new Guid("2c709ca2-17cf-4a47-bc93-93d3b27d4abd");   // Retrieves web part properties         

PropertyValues webpartProps = clientContext.Site.RootWeb.GetWebPartProperties(webpartId, pageUrl);  

// Displays results         

Console.WriteLine("Title : " + webpartProps["Title"]);          

Console.ReadKey();      

}  

}  

catch (Exception ex)  

{      

Console.WriteLine("Error Message: " + ex.Message);      

Console.ReadKey();  

}  

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 NameProperty ValuePage URLWeb Part ID (GUID)

Display the results.The following code shows the operation. // Input Parameters    

string siteUrl = "https://nakkeerann.sharepoint.com";  

string userName = "abc@abc.onmicrosoft.com"; 

 string password = "***";  

// PnP component to set context     AuthenticationManager authManager = new AuthenticationManager();  

try

{  

// Get and set the client context

// Connects to SharePoint online site using inputs provided using (var clientContext = authManager.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))      

{  

// Input Parameters

string pageUrl = "/Pages/TestPage.aspx";          

Guid webpartId = new Guid("2c709ca2-17cf-4a47-bc93-93d3b27d4abd");   string propertyName = "Title";  

string propertyValue = "Title updated";  

// Updates the web part properties         

clientContext.Site.RootWeb.SetWebPartProperty(propertyName, propertyValue, webpartId, pageUrl);   // Displays the results         

Console.WriteLine("Updated the properties of a web part on page");          

Console.ReadKey();      

}  

}  

catch (Exception ex)  

{      

Console.WriteLine("Error Message: " + ex.Message);      

Console.ReadKey();  

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

Recent Posts

See All