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

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;
        break;
    }
}

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
add comment

1 Answer 1

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
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.