Showing posts with label Page Layout. Show all posts
Showing posts with label Page Layout. Show all posts

Sunday, August 7, 2011

Set Page Layout programatically for a publishing page

If you like to create pages in site through code then you have to set the page layout for the page. Below is the simple code which does that works well.
private static void SetPageLyoutToPublishibPage()
        {
            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;
                        }

                    PublishingPage page = publishingWeb.GetPublishingPage(web.ServerRelativeUrl + "/Pages/Default.aspx");
                    page.CheckOut();
                    page.Layout = pageLayout;
                    page.Update();
                    page.CheckIn("");
                }
            }
        }
Any issues, please post it here.

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...

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. :)