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

In CMOS we can get the webparts from a page using ‘Microsoft.SharePoint.Client.WebParts.LimitedWebPartManager’ object. We can get all webparts information by using ‘LimitedWebPartManager’ except for the modern/client site pages.

We can get modern page webparts by using OfficeDevPnP.Core. In this blog, I am going to share all codes on how we can get all webparts information by using OfficeDevPnP.Core.

First of all, we have installed OfficeDevPnP.Core dlls by using Nuget package. Go to solution explorer and click on ‘Manage Nuget Package’. Then navigate to ‘Browse’ panel and paste ‘SharePointPnPCoreOnline’ in the search box.

You can see ‘SharePointPnPCoreOnline’, please select that can click on Install button present on the right side of that page. I have added the necessary screen-shots for better understanding.

I am using a simple Console application to iterate all client-side webparts information.

Please follow the below codeā€¦

using System;
using OfficeDevPnP.Core;
using OfficeDevPnP.Core.Pages;
using Microsoft.SharePoint.Client;

namespace ConsoleApp4
{
    class Program
    {
        static void Main(string[] args)
        {
            Web web = null;
            File file = null;
            ListItem item = null;
            List sitePagesList = null;
            ClientContext ctx = null;
            ClientSideWebPart clientSideWebPart = null;

            string siteUrl = string.Empty;
            string userName = string.Empty;
            string password = string.Empty;

            //Initializing AuthenticationManager
            AuthenticationManager authMgr = new AuthenticationManager();

            //Site Url to scan
            siteUrl = "SiteUrlToScan";

            //Login User name
            userName = "UserName";

            //Login password
            password = "Password";

            //Getting SharePoint Client Context by using PNP Core
            using (ctx = authMgr.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))
            {
                try
                {
                    web = ctx.Web;

                    //loading sharepoint web instance
                    ctx.Load(web);
                    ctx.ExecuteQueryRetry();

                    //get list by using list name
                    sitePagesList = web.Lists.GetByTitle("Site Pages");

                    //getting item by using item id
                    item = sitePagesList.GetItemById(1);

                    ctx.Load(item, I => I.File, I => I.DisplayName);
                    ctx.ExecuteQueryRetry();

                    file = item.File;
                    var page = ClientSidePage.Load(ctx, file.Name);
                    var webParts = page.Controls.FindAll(c => c.Type.Name == "ClientSideWebPart");

                    if (webParts != null && webParts.Count > 0)
                    {
                        foreach (var webpart in webParts)
                        {
                            try
                            {
                                clientSideWebPart = (ClientSideWebPart)webpart;
                                Console.WriteLine(clientSideWebPart.Title);
                            }
                            catch (Exception ex) { }
                        }
                    }

                    Console.ReadLine();
                }
                catch (Exception ex) { }
            }
        }
    }
}

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

This solution is brought to you by our SharePoint professionals.

Softree Technology employs SharePoint consultants; we are a technology services provider with the aim to help companies achieve exceptional performance through SharePoint. Our dedicated team of SharePoint consultants has the right bent of mind to understand and execute customer requirements.

Be it SPFx or SharePoint add-in developments, SharePoint 2019 developments, web part developments, migrating from SharePoint 2010/2013 to SharePoint 2013/2016/Office 365, Office 365, SharePoint hosted apps development or something else in SharePoint, we strive to deliver the best

Tags: , , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *