Jun
16
2008

SPFile.CustomizedPageStatus & SPFile.RevertContentStream - The way to check SPFile Ghost/Un-Ghost status and Revert to site definition via code

In my previous post I've discussed how to modify page layout properties through code (in feature activated event). One thing to note is that in the last part of code sample, it requires you to perform following steps to get page layout properties updated.

1. Check out page layout
2. Call page layout .Update()
3. Check in page layout

What I didn't talk about in the post is that after you Check-out the page layout file and check it back in, the actual file gets un-ghost from server.

(If you don't know what ghost / un-ghost are, there are many great articles you can find and read about it over Internet)

Now let's examine closer to see what happens to the layout files after code execution.

Before check-out

James Tsai .Net  SharePoint VSTO ASP.Net blog - MasterPageGallery page layouts

Above displays all page layouts available to team site by default.

Here we check each page layout Ghost status without any modification made to the file:

using (SPSite site = new SPSite(siteUrl))
{
    using (SPWeb web = site.OpenWeb())
    {
        PublishingSite publishingSite = new PublishingSite(site);
        PageLayoutCollection pageCollection = publishingSite.PageLayouts;
        foreach (PageLayout layout in pageCollection)
        {
            SPFile currentFile = web.GetFile(layout.ServerRelativeUrl);

            Console.WriteLine("{0} --before checkout-- {1}",layout.Name, currentFile.CustomizedPageStatus);

        }
    }
}

James Tsai .Net  SharePoint VSTO ASP.Net blog - ghosted page layouts

You can see that CustomizedPageStatus  is "Uncustomized" means that the file is in Ghost status.

After Modify and Check-in

Now, let's try update each page layout's description text and check it back in, then we check its ghost status again.

foreach (PageLayout layout in pageCollection)
{
    layout.Description = "James.Tsai.Net -"+layout.Description;
    SPFile currentFile = web.GetFile(layout.ServerRelativeUrl);
    currentFile.CheckOut();
    layout.Update();
    currentFile.CheckIn("");

    Console.WriteLine("{0} --after update-- {1}",layout.Name, currentFile.CustomizedPageStatus);

}

All descriptions are updated

James Tsai .Net  SharePoint VSTO ASP.Net blog - MasterPageGallery page layouts description

But if you check all layouts Ghost status again, you see they are now in un-ghost status (Customized)

James Tsai .Net  SharePoint VSTO ASP.Net blog - unghosting page layouts

Make it ghost file again

To set them back to ghost status, we can use "Reset to site definition" option in site setting page. If you don't want to do it from UI, SPFile also provided a method for you to do it from code.

SPFile.RevertContentStream()

foreach (PageLayout layout in pageCollection)
{                       
    SPFile currentFile = web.GetFile(layout.ServerRelativeUrl);
    currentFile.RevertContentStream();

    Console.WriteLine("{0} --after revert-- {1}", layout.Name, currentFile.CustomizedPageStatus);
}

James Tsai .Net  SharePoint VSTO ASP.Net blog - reset to page layouts definition

For all the templates file & page layouts, they should always in ghost status for both performance and consistency across sites. If you know somewhere in your code breaks ghosting, don't forget to revert it back to its ghost status. You can always check its status by calling SPFile.CustomizedPageStatus().

blog comments powered by Disqus