Get All Client Side WebParts From A SharePoint Modern Page Using PnP Core/ How To Extract Modern Page WebParts Using PnP Core

We can get all the web parts information by using 'LimitedWebPartManager' except for the modern/client site pages.

In CMOS, we can get the web parts from a page using 'Microsoft.SharePoint.Client.WebParts.LimitedWebPartManager' object. We can get all the web parts' information by using 'LimitedWebPartManager' except for the modern/client site pages.
 
We can get modern page web parts by using OfficeDevPnP.Core. In this blog, I am going to share all codes on how we can get all the web parts information by using OfficeDevPnP.Core.
 
First of all, we have installed OfficeDevPnP.Core DLLs by using the NuGet package. Go to the Solution Explorer and click on 'Manage Nuget Package'. Then, navigate to the 'Browse' panel and paste 'SharePointPnPCoreOnline' in the search box.
 
Get All Client Side WebParts From A SharePoint Modern Page Using PnP Core/ How To Extract Modern Page WebParts Using PnP Core
 
You can see 'SharePointPnPCoreOnline'. Please select that and click on the "Install" button present on the right side of that page. I have added the necessary screenshots for better understanding.
 
Get All Client Side WebParts From A SharePoint Modern Page Using PnP Core/ How To Extract Modern Page WebParts Using PnP Core
 
I am using a simple console application to iterate all client-side web parts information.
 
Please follow the below code.
  1. using System;  
  2. using OfficeDevPnP.Core;  
  3. using OfficeDevPnP.Core.Pages;  
  4. using Microsoft.SharePoint.Client;  
  5. namespace ConsoleApp4 {  
  6.     class Program {  
  7.         static void Main(string[] args) {  
  8.             Web web = null;  
  9.             File file = null;  
  10.             ListItem item = null;  
  11.             List sitePagesList = null;  
  12.             ClientContext ctx = null;  
  13.             ClientSideWebPart clientSideWebPart = null;  
  14.             string siteUrl = string.Empty;  
  15.             string userName = string.Empty;  
  16.             string password = string.Empty;  
  17.             //Initializing AuthenticationManager  
  18.             AuthenticationManager authMgr = new AuthenticationManager();  
  19.             //Site Url to scan  
  20.             siteUrl = "SiteUrlToScan";  
  21.             //Login User name  
  22.             userName = "UserName";  
  23.             //Login password  
  24.             password = "Password";  
  25.             //Getting SharePoint Client Context by using PNP Core  
  26.             using(ctx = authMgr.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password)) {  
  27.                 try {  
  28.                     web = ctx.Web;  
  29.                     //loading sharepoint web instance  
  30.                     ctx.Load(web);  
  31.                     ctx.ExecuteQueryRetry();  
  32.                     //get list by using list name  
  33.                     sitePagesList = web.Lists.GetByTitle("Site Pages");  
  34.                     //getting item by using item id  
  35.                     item = sitePagesList.GetItemById(1);  
  36.                     ctx.Load(item, I => I.File, I => I.DisplayName);  
  37.                     ctx.ExecuteQueryRetry();  
  38.                     file = item.File;  
  39.                     var page = ClientSidePage.Load(ctx, file.Name);  
  40.                     var webParts = page.Controls.FindAll(c => c.Type.Name == "ClientSideWebPart");  
  41.                     if (webParts != null && webParts.Count > 0) {  
  42.                         foreach(var webpart in webParts) {  
  43.                             try {  
  44.                                 clientSideWebPart = (ClientSideWebPart) webpart;  
  45.                                 Console.WriteLine(clientSideWebPart.Title);  
  46.                             } catch (Exception ex) {}  
  47.                         }  
  48.                     }  
  49.                     Console.ReadLine();  
  50.                 } catch (Exception ex) {}  
  51.             }  
  52.         }  
  53.     }  
  54. }  

In my code example, I have three webparts in Home.aspx page as mentioned in the below screen-shots and also after running the above code you can find all three client-side webparts in my result screen-shot.

Get All Client Side WebParts From A SharePoint Modern Page Using PnP Core/ How To Extract Modern Page WebParts Using PnP Core
 
Get All Client Side WebParts From A SharePoint Modern Page Using PnP Core/ How To Extract Modern Page WebParts Using PnP Core