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;
    PublishingWeb objPublishingWeb = PublishingWeb.GetPublishingWeb(web);
    //Set default page layout of site to internal page layout               
    if (objPublishingWeb != null)
    {
       PageLayout _pageLayout = (from _pl in objPublishingWeb.GetAvailablePageLayouts()
                               where _pl.Name == “InnerPage.aspx”
                               select _pl).FirstOrDefault();
       objPublishingWeb.SetDefaultPageLayout(_pageLayout, true);
       objPublishingWeb.Update();
    }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s