Get page, its pagelayouts and webparts using CSOM

Annam, Sachchin 21 Reputation points
2021-06-16T16:57:36.833+00:00

Hello Techies,
For some analysis, I must retrieve all the pages from SPO classic along with the layout attached to it and the numbers of webparts that page has.

I wrote this basic code, however I am unable to proceed further for pagelayouts and webparts

 var clientContext = GetonlineContext(sitecollectionURL);
            Web oWeb = clientContext.Web;
            List oPages = clientContext.Web.Lists.GetByTitle("Pages");
            ListItemCollection oColl = oPages.GetItems(CamlQuery.CreateAllItemsQuery());
            clientContext.Load(oColl);
            clientContext.Load(oWeb, w => w.Title, w => w.Webs, w => w.Lists, w => w.Url);
            clientContext.ExecuteQuery();

            foreach(ListItem item in oColl)
            {
                clientContext.Load(item, (System.Linq.Expressions.Expression<Func<ListItem, object>>)item.FieldValues["PublishingPageLayout"]);
                clientContext.ExecuteQuery();
                Console.WriteLine("File Ref: " + item["FileRef"]);
                Console.WriteLine("Page Layout: " + ((FieldUrlValue)item.FieldValues["PublishingPageLayout"]).Description);


            }
SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
11,230 questions
0 comments No comments
{count} votes

Accepted answer
  1. MichaelHan-MSFT 18,126 Reputation points
    2021-06-18T07:47:23.977+00:00

    Hi @Annam, Sachchin ,

    You could check if the page laouts exist like the below:

                var layout = (FieldUrlValue)item.FieldValues["PublishingPageLayout"];  
                if (layout !=null)   
                {   
                    Console.WriteLine("Page Layout: " + (layout.Description));  
                }  
                else  
                {  
                    Console.WriteLine("Page Layout: none");  
                }
    
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. MichaelHan-MSFT 18,126 Reputation points
    2021-06-17T02:33:03.153+00:00

    Hi @Annam, Sachchin ,

    We could use File.GetLimitedWebPartManager method to retrieve the webparts in the page. Below is my sample code for you:

            foreach (ListItem item in oColl)  
            {  
                ctx.Load(item);  
                ctx.ExecuteQuery();  
                Console.WriteLine("File Ref: " + item["FileRef"]);  
                Console.WriteLine("Page Layout: " + ((FieldUrlValue)item.FieldValues["PublishingPageLayout"]).Description);  
                File file = item.File;  
                ctx.Load(file);  
                ctx.ExecuteQuery();  
                var wpManager = file.GetLimitedWebPartManager(Microsoft.SharePoint.Client.WebParts.PersonalizationScope.Shared);  
                var webparts = wpManager.WebParts;  
                ctx.Load(webparts);  
                ctx.ExecuteQuery();  
                Console.WriteLine("Webparts count: " + webparts.Count);  
                if (webparts.Count > 0)  
                {  
                    foreach (var webpart in webparts)  
                    {  
                        ctx.Load(webpart.WebPart.Properties);  
                        ctx.ExecuteQuery();  
                        var propValues = webpart.WebPart.Properties.FieldValues;  
    
                        foreach (var property in propValues)  
                        {  
                            if (property.Key == "Title")  
                            {  
                                Console.WriteLine("Title: " + property.Value);  
                            }  
                            if (property.Key == "Description")  
                            {  
                                Console.WriteLine("Description: " + property.Value);  
                            }  
                        }  
                    }  
                }  
            }  
    

    If an Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


    If the response is helpful, please click "Accept Answer" and upvote it.

    0 comments No comments

  2. Annam, Sachchin 21 Reputation points
    2021-06-17T14:09:58.137+00:00

    @MichaelHan-MSFT
    Thanks for the response, i was able to proceed further to get page layouts, webparts and webpart type. However there are few pages where there is no pagelayout and the code breaks

    it breaks here:
    Console.WriteLine("page Layout: " + ((FieldUrlValue)item["PublishingPageLayout"]).Description);

    if (item["FileRef"].ToString().Contains(".aspx"))  
                    {  
                        Console.WriteLine("page Layout: " + ((FieldUrlValue)item["PublishingPageLayout"]).Description);  
                        File spfile = clientContext.Web.GetFileByServerRelativeUrl(item["FileRef"].ToString());  
                        clientContext.Load(spfile);  
                        clientContext.ExecuteQuery();  
                        LimitedWebPartManager wpManager = spfile.GetLimitedWebPartManager(PersonalizationScope.Shared);  
                        var webparts = wpManager.WebParts;  
                        clientContext.Load(webparts);  
                        clientContext.ExecuteQuery();  
                        if (webparts.Count > 0)  
                        {  
                            foreach (var webpart in webparts)  
                            {  
                                Guid webPartId = webpart.Id;  
                                ClientResult<string> webPartXml = wpManager.ExportWebPart(webPartId);  
                                clientContext.Load(webpart.WebPart.Properties);  
                                clientContext.ExecuteQuery();  
                                Console.WriteLine(webPartXml.Value);  
                                var webpartTypeName = getWebpartType(webPartXml.Value);  
                                Console.WriteLine("Webpart Type: "+webpartTypeName);  
      
                                var propValues = webpart.WebPart.Properties.FieldValues;  
                                foreach (var property in propValues)  
                                {  
                                    if (property.Key == "Title")  
                                    {  
                                        Console.WriteLine("Title: " + property.Value);  
                                    }  
                                    if (property.Key == "Description")  
                                    {  
                                        Console.WriteLine("Type: " + property.Value);  
                                    }  
                                }  
      
                            }  
                        }  
                    }  
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.