question

AnnamSachin-1678 avatar image
0 Votes"
AnnamSachin-1678 asked MichaelHan-MSFT answered

Get page, its pagelayouts and webparts using CSOM

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);
    
    
             }
office-sharepoint-online
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MichaelHan-MSFT avatar image
0 Votes"
MichaelHan-MSFT answered

Hi @AnnamSachin-1678,

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");
             }
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MichaelHan-MSFT avatar image
0 Votes"
MichaelHan-MSFT answered

Hi @AnnamSachin-1678,

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.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AnnamSachin-1678 avatar image
0 Votes"
AnnamSachin-1678 answered

@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);
                                 }
                             }
    
                         }
                     }
                 }
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.