Sunday, August 7, 2011

Set Page Layout programatically for a publishing page

If you like to create pages in site through code then you have to set the page layout for the page. Below is the simple code which does that works well.
private static void SetPageLyoutToPublishibPage()
            using (SPSite site = new SPSite("http://SP2010Site"))
                using (SPWeb web = site.OpenWeb())
                    PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(web);
                    PageLayout pageLayout = null;
                    foreach (PageLayout p in publishingWeb.GetAvailablePageLayouts())
                        if (p.Name.Equals("BlankWebPartPage.aspx", StringComparison.InvariantCultureIgnoreCase))
                            pageLayout = p;

                    PublishingPage page = publishingWeb.GetPublishingPage(web.ServerRelativeUrl + "/Pages/Default.aspx");
                    page.Layout = pageLayout;
Any issues, please post it here.

No comments:

Post a Comment