Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

Wondered if it's possible to iterate through the pages held in the pages library and determine the page layout being used by each page? any c# code examples appreciated.

Many thanks in advance

share|improve this question
add comment

2 Answers

I believe you need to use PublishingPage.GetPublishingPage to get the page, then use that to get Layout.

For example:

var pagesList = web.Lists["Pages"];
pagesList.GetItems(new SPQuery()).Cast<SPListItem>().ToList().ForEach(item => {
  if (!PublishingPage.IsPublishingPage(item)) return;

  var publishingPage = PublishingPage.GetPublishingPage(item);
  Console.WriteLine(string.Format("Page '{0}' use layout named '{1}' with title '{2}'", 
     publishingPage.Title,
     publishingPage.Layout.Name, 
     publishingPage.Layout.Title));
});
share|improve this answer
    
I'm trying something similar to test this but keep getting a "Value does not fall within expected range error" says the SPListItem is not compatible with a PublishingPage: SPList MyPages = web.Lists["Pages"]; SPListItemCollection MyPagesColl = MyPages.GetItems(Customview); foreach(int i=0; i<MyPagesColl.Count;i++) { SPListItem item = MyPagesColl[i]; //error occurs here PublishingPage Page = PublishingPage.GetPublishingPage(item); TestLabel.Text = Page.Layout.Name; } –  user574204 Nov 11 '12 at 19:30
    
That foreach looks very suspicious, have you tried foreach (var item in myPagesColl){...}? And what does PublishingPage.IsPublishingPage(item)) give you? –  eirikb Nov 11 '12 at 21:14
add comment

Use SPSiteDataQuery to get all the pages or filter them w.r.t their associated Content Types. See example below

SPWeb web = SPContext.Current.Site.RootWeb;
SPSiteDataQuery query = new SPSiteDataQuery();

//Server template for pages library is 850
query.Lists = "<Lists ServerTemplate='850' Hidden='TRUE' />";  

query.ViewFields = @"<FieldRef Name='URL' Nullable='True' Type='URL' /><FieldRef Name='Title' Nullable='TRUE' Type='Text' />";

query.Webs = "<Webs Scope='Recursive'  />";
query.Query = @"<Where><Eq><FieldRef Name='ContentType'/><Value Type='Text'>CT_Page_News</Value></Eq></Where>"
DataTable dt = web.GetSiteData(query);

SPSiteDataQuery: this class used for cross site query. Query a data within site collection or specific web.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.