Friday, 4 July 2008

Uncustomising MOSS Pages - Part 2

The other day I wrote about the frustrations of uncustomising a page layout deployed via a feature to the master page gallery. I've since done some additonal hunting and come across the SPFile.RevertContentStream ( ) method to revert a page layout or master page to its original state so feature deployments work again.

Calling this successfully reverts/uncustomises/reghosts the file in question, although I did notice the version history accessible through the master page gallery remains as-is. Nevertheless, a change to the file system .aspx file after running this code is honoured by MOSS.

Here's the code I wrote to encapsulate that call (it's a bit rough but for a one-off excercise does the trick). I simply run this from a console application (which you can download from here).

// Wait for debugger to attach
Console.WriteLine ("Waiting for debugger to attach. Press any key to continue...");
Console.ReadLine ();

const string FILE_PATH = "/_catalogs/masterpage/ThreeColumns.aspx";

using (SPSite site = new SPSite ("http://dev:30000/"))
{
using (SPWeb web = site.AllWebs ["/"])
{
SPFile file = web.GetFile (FILE_PATH);

Console.WriteLine ("File status before change: " +
file.CustomizedPageStatus);

if (file != null &&
file.Exists &&
file.CustomizedPageStatus == SPCustomizedPageStatus.Customized)
{
Console.WriteLine ("Reverting file...");

try
{
file.RevertContentStream ();
}
catch (Exception e)
{
Console.WriteLine (e);
}

Console.WriteLine ("File status after change: " +
file.CustomizedPageStatus);
}
else
{
Console.WriteLine ("Unable to retrieve file");
}
}
}

2 comments:

  1. Very nice. Thanks for the posts. I am sure this will come in handy for me in the future...

    ReplyDelete
  2. YOU R THE MAN!
    I happened to do the same thing accidentally to 1 of feature-ed layout and was worried there is no way back... Thank you thank you thank you.

    ReplyDelete

Spam comments will be deleted

Note: only a member of this blog may post a comment.