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

I have code as part of an event receiver that creates a publishing page upon feature activation. That publishing page is built on a page layout that is deployed as part of that same feature.

The pages.Add method of the PublishingPage object requires a PageLayout object of the layout you wish to have the page created with. I'm currently using the following code to identify and set the PageLayout object.

PublishingWeb activePublishingWeb = PublishingWeb.GetPublishingWeb(web);
PublishingPageCollection pages = activePublishingWeb.GetPublishingPages();
PageLayout customLayout = null;
foreach (PageLayout layout in activePublishingWeb.GetAvailablePageLayouts())
    if (layout.Name.Equals("MyLayout.aspx", StringComparison.InvariantCultureIgnoreCase))
        customLayout = layout;

This method of grabbing all of the page layouts and iterating through them works, but it seems rather inefficient. Is there a better way to specifically target a page layout using a name or a URL? I know you can filter the PageLayout return by associated content type, but that still gives me a collection of all Welcome Pages, not just my custom one.

Any suggestions are appreciated!

With Per's suggestion below, here's the new code for anyone that may stumble upon this thread in the future:

SPFile customLayoutFile = web.GetFile("_catalogs/masterpage/MyLayout.aspx");
SPListItem customLayoutItem = customLayoutFile.Item;
PageLayout customLayout = new PageLayout(customLayoutItem);
share|improve this question
up vote 2 down vote accepted

If you have the url of the PageLayout you can use SPWeb.GetFile(url) to get the corresponding SPFile and the use the PageLayout constructor on the Item property of that SPFile

share|improve this answer
That did it; thank you Sir! Original post updated with new solution. – webdes03 Oct 22 '12 at 18:14

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.