Sunday, August 7, 2011

Get default Page Layout for a SharePoint site

Sometimes when you are provisioning sites through web/site templates you might miss one thing that setting default page layout for the web. In SharePoint 2010 especially you have to face this issue when you try to browse to the page "Page layouts and Site Templates" from site settings page. If there is no default page layout then there are problems while creating new page as well. So, this could be a major issue in some special cases.

Sometimes you might get the error like "Data at the root level is invalid. Line 1, position 1" because of this.
So, we have to know is there any default page layout set for the site and below is the perfect console application solution for it.
        private static void GetDefaultPageLayout()
        {
            using (SPSite site = new SPSite("http://SP2010Site/"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(web);

                    PageLayout pageLayout = publishingWeb.DefaultPageLayout;
                    Console.WriteLine(pageLayout.Name + "Url : " + pageLayout.ServerRelativeUrl);
                }
            }
        }
So, this way you can trace easily some kind of problems which create problems to us. :)

No comments:

Post a Comment