SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

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}'", 
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

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

Your Answer


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.