I ran into an issue where several of my Page Layouts had been customized using SharePoint Designer. I didn’t want them to be customized, because I had deployed an upgraded solution file, which had updated the Page Layout Feature on the file system; if the pages remained customized in SPD, they wouldn’t “pick up” the new changes on the file system. I wrote a short command-line application, and I thought I’d post the code so you could do it, too, if needed.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Publishing;

namespace CTP.SharePoint.Publishing.RevertPageLayouts
{
    class Program
    {
        static void Main(string[] args)
        {
            //Make sure the user entered a site collection URL
            if (args.Length < 1)
            {
                Console.WriteLine("Please enter a Site Collection URL.");
                Console.Read();
                return;
            }

            try
            {
                string url = args[0];
                using (SPSite siteCollection = new SPSite(url))
                {
                    PublishingSite pubSite = new PublishingSite(siteCollection);
                    PageLayoutCollection pageLayouts = pubSite.GetPageLayouts(true);
                    foreach (PageLayout pageLayout in pageLayouts)
                    {
                        if (pageLayout.ListItem.File.CustomizedPageStatus == SPCustomizedPageStatus.Customized)
                        {
                           //Make sure the Page Layout hasn't been checked out. (If it is checked out, this will skip that file.)
                            if (pageLayout.ListItem.File.CheckOutStatus == SPFile.SPCheckOutStatus.None)
                            {
                                pageLayout.ListItem.File.CheckOut();
                                try
                                {
                                    pageLayout.ListItem.File.RevertContentStream();
                                    pageLayout.Update();

                                    Console.WriteLine("Reverted " + pageLayout.Title);
                                }
                                catch (Exception exc)
                                {
                                    Console.WriteLine("An error occurred with layout " + pageLayout.Title + ". Error " + exc.Message);
                                }
                                pageLayout.ListItem.File.CheckIn("Reverted page layout to original");
                            }                          
                        }
                    }
                }
                Console.Read();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.Read();
            }
        }
    }
}
About Admin

Owner of Savvy Technical Solutions

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.