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.
-
- string siteUrl = "https://nakkeerann.sharepoint.com";
- string userName = "[email protected]";
- string password = "***";
-
-
- AuthenticationManager authManager = new AuthenticationManager();
- try
- {
-
-
- using (var clientContext = authManager.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))
- {
-
- string pageUrl = "/Pages/TestPage.aspx";
- Guid webpartId = new Guid("2c709ca2-17cf-4a47-bc93-93d3b27d4abd");
-
- PropertyValues webpartProps = clientContext.Site.RootWeb.GetWebPartProperties(webpartId, pageUrl);
-
-
- 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 Name
- Property Value
- Page URL
- Web Part ID (GUID)
- Display the results.
The following code shows the operation.
-
- string siteUrl = "https://nakkeerann.sharepoint.com";
- string userName = "[email protected]";
- string password = "***";
-
-
- AuthenticationManager authManager = new AuthenticationManager();
- try
- {
-
-
- using (var clientContext = authManager.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))
- {
-
- string pageUrl = "/Pages/TestPage.aspx";
- Guid webpartId = new Guid("2c709ca2-17cf-4a47-bc93-93d3b27d4abd");
-
- string propertyName = "Title";
- string propertyValue = "Title updated";
-
-
- clientContext.Site.RootWeb.SetWebPartProperty(propertyName, propertyValue, webpartId, pageUrl);
-
-
- 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.