Change PublishingPageLayout of Page Programmatically


After the launch of SharePoint 2010 we all learned that the platform is improved heavily in all aspects including enhancements in web content management.

We can easily change the layout of any Publishing Page using a ribbon in SharePoint 2010.

But many of us are still confused about how we can do this using MOSS 2007? Intead some people think that we cannot change the page layout of a page using MOSS 2007, but this is not true.

Well, to know how to do this in MOSS 2007, here are some steps.

  1. Browse the page for which you want to change the page layouts (of course this should be a publishing page).

  2. If you are working with Publishing Site, click on Site Actions and select Show Page editing toolbar (if you are working with custom master pages, and see that this option is grayed out then make sure that you have added PublishingConsole control entry on the master page).

  3. Now you will be able to see the Page Editing tool bar; then select the Page option; a drop down will appear, there you have to select Page Settings and Schedule option.

  4. After selecting this you will see a page will be opened; scroll down and find the option Page Layout.

  5. Now this is where you can apply page layouts to a page; select a different page layout from the dropdown and click ok.

I hope now your previous page layout for the page is changed. But there are some cases I encoutered where I wanted to change the layout of the page but after doing all these steps when I reached the page for changing the layouts, I didn't find the dropdown filled with other page layouts, so I was not able to change it.

So what to do in that case?

Well as a developer, we always have code to help us, so I decided to do these all using the SP object model.

And here is the code that I have done; this works for me at least.

The code just tries to open the root web, gets the collection of pages created with a given page layout, searches the given page in the collection, and applies a new given page layout for the page and then it is  done.

There can be other ways to do this, and this code can be modified for more flexibility and according to requirements.

static void Main(string[] args)

{

 #region Variables

 

 string _siteUrl = string.Empty;

 string _pageName = string.Empty;

 string _pageLayouts = string.Empty;

 string _newPageLayout = string.Empty;

 string _responce = string.Empty;

 PublishingPageCollection _pages = null;

 

 #endregion

 

 try

 {

   Console.WriteLine("Site Url: ");

   _siteUrl = Console.ReadLine();

 

   if (!string.IsNullOrEmpty(_siteUrl))

   {

    SPSecurity.RunWithElevatedPrivileges(delegate()

    {

      using (SPSite site = new SPSite(_siteUrl))

      {

        using (SPWeb web = site.RootWeb)

        {

          if (web != null)

          {

           if (PublishingWeb.IsPublishingWeb(web))

           {

             PublishingWeb pWeb = PublishingWeb.GetPublishingWeb(web);

             Console.WriteLine("Search Pages with Layout: ");

             _pageLayouts = Console.ReadLine();

 

             if (!string.IsNullOrEmpty(_pageLayouts))

             {

string query = @"<Where><Contains><FieldRef   Name='PublishingPageLayout' /><Value Type='URL'>" + _pageLayouts + "</Value></Contains></Where>";

                 

_pages = pWeb.GetPublishingPages(query);

             }

 

             do

             {

              if (_pages != null)

              {

               Console.WriteLine();

               Console.WriteLine("Search Page With Name: ");

               _pageName = Console.ReadLine();

 

               //LINQ to get actual page for processing

PublishingPage _desiredPage = _pages.Where(p => p.Name.ToLower().Contains(_pageName.ToLower())).FirstOrDefault();

 

                                              

 

   if (_desiredPage != null)

               {

                if (_desiredPage.ListItem.File.Level != SPFileLevel.Checkout)

                {

                                                        Console.WriteLine("Processing.." + _desiredPage.Name);

                                                        _desiredPage.ListItem.File.CheckOut();

 

                  Console.WriteLine("which Page Layout to apply?");

                  _newPageLayout = Console.ReadLine();

 

_desiredPage.ListItem["PublishingPageLayout"] = @"/_catalogs/masterpage/" + _newPageLayout;

                                                        _desiredPage.ListItem.Update();

 

_desiredPage.ListItem.File.CheckIn(string.Empty);

 

                  if (pWeb.PagesList.EnableMinorVersions)

                  {

_desiredPage.ListItem.File.Publish("Page Layout Changed..");

                  }

                   if (pWeb.PagesList.EnableModeration)

                   {

_desiredPage.ListItem.File.Approve("Approved by Console Application");

                   }

 

                   Console.WriteLine("Page Layout Updated");

 

                   Console.WriteLine("do you want to continue?");

                    _responce = Console.ReadLine();

 

                  }

                else

                {

Console.WriteLine("Page is already Checked out to user " + _desiredPage.ListItem.File.CheckedOutBy.Name);

                }

               }

               else

               {

                 Console.WriteLine("Page Not Found!!");

               }

              }

             }

while (_responce.ToLower().Equals("y") || _responce.ToLower().Equals("yes"));

 

            if (pWeb != null)

            {

              pWeb.Close();

            }

 

           }

          }

        }

       }

      });

     }

     else

     {

      Console.WriteLine("Invalid Site Url");

     }

 

    }

    catch (Exception ex)

    {

      Console.WriteLine("{0}:{1}", "Error", ex.Message);

    }

 

     Console.ReadLine();

   }