Blogger

  • This email address is being protected from spambots. You need JavaScript enabled to view it.

    Recent items

Login

Thursday, 31 January 2013 23:05

Programmatically change the PageLayout (and the associated ContentType) to a PublishingPage

Written by
Rate this item
(0 votes)

The function above show how to change programmatically the PageLayout (and the associated ContentType) to a PublishingPage.

Show/Hidden csharp code

View source
 
 
 
private static void ModifyPageLayout(string siteUrl, string pageName, string pageLayoutName)
{
    using (SPSite site = new SPSite(siteUrl))
    {
        using (SPWeb myWeb = site.OpenWeb())
        {
            PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(myWeb);
            
            PublishingPage page = publishingWeb.GetPublishingPage(siteUrl + "/pages/" + name);
            if (!string.IsNullOrEmpty(templateName) && page!=null)
            {
                PageLayout pl = page.PublishingWeb.GetAvailablePageLayouts().Where(p => string.Compare(p.Name, pageLayoutName, true) == 0).FirstOrDefault();
                if (pl != null)
                {
                    page.CheckOut();
                    page.Layout = pl;
                    
                    // Now set the content type
                    page.ListItem[SPBuiltInFieldId.ContentTypeId] = pl.AssociatedContentType.Id;
                    page.ListItem[SPBuiltInFieldId.ContentType] = pl.AssociatedContentType.Name;
 
                    page.Update();
                }
            }
        }
    }
}
 
 
 

Read 27782 times Last modified on Tuesday, 12 January 2016 10:43

3 comments

Leave a comment

Make sure you enter all the required information, indicated by an asterisk (*). HTML code is not allowed.