Sunday, August 7, 2011

Set default Page Layout for a SharePoint site

Before reading this post, please take a look at this post: Get default Page Layout for a SharePoint site.
Setting default page layout to a SharePoint site is very important. For example if you are trying to create a new site/web template in SharePoint and from it you like to create sites then do not forget to set default page layout [especially in SharePoint 2010].
private static void SetDefaultPageLayout()
        {
            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;
                            break;
                        }
                    publishingWeb.SetDefaultPageLayout(pageLayout, true);
                    publishingWeb.Update();
                }
            }
        }
Here, I have set the Blank Web Part page as the default page layout to the SharePoint site. This way you can control the logic as your wish...

1 comment:

  1. thanks a lot man...your article ...saved my day

    ReplyDelete