Pages

Search This Blog

Tuesday, February 21, 2012

Programatically set default Page layout of site to Custom page layout

In my previous post, we have learned how to create custom page layouts using feature.

Requirement:
We need to set the custom page layout created as the default page layout of the site.
Say if a user creates a new page, it should create a page which is using the custom layout.
We need to achieve this programatically.

Solution:

We can achieve this by writing the code within the receiver of the feature created.

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{   
    SPSite site = properties.Feature.Parent as SPSite;
    SPWeb web = null;
    if (site == null)
    {
         web = properties.Feature.Parent as SPWeb;
         if (web == null) return;
         site = web.Site;
    }
    else web = site.RootWeb;

       //Get the publishing web object
    PublishingWeb objPublishingWeb = PublishingWeb.GetPublishingWeb(web);
    //Set default page layout of site to internal page layout  (InnerPage.aspx)             
    if (objPublishingWeb != null)
    {
       PageLayout _pageLayout = (from _pl in objPublishingWeb.GetAvailablePageLayouts()
                               where _pl.Name == "InnerPage.aspx"
                               select _pl).FirstOrDefault();
       objPublishingWeb.SetDefaultPageLayout(_pageLayout, true);
       objPublishingWeb.Update();
    }
}


No comments:

Post a Comment