Thursday, July 11, 2013

Ghosting the unghosted master page

Sometimes master page files in SharePoint libraries can become unghosted, meaning they are no longer linked to a physical file in the file system. This can happen when someone, for example, edits a master page using SharePoint Designer.

A file inside a library have have one out of three different states when looking at its state using PowerShell and the SPFile.CustomizedPageStatus property:
  • customized
  • un-customized
  • none
Customized (unghosted) means the file is no longer linked to a physical file, un-customized (ghsoted) means the file is linked to a local file (and if needed can by updated via a solutions package) and none means the file never had a file linked to it.

If you are updating through a wsp package a master page and notice the currently installed master page is in the "none" state, proceed with first renaming the current master page. Note: this will not break the pages that are currently using it. Pages use a reference to master pages, changing their names will not change the reference. Next install/update the package that contains the update master page. If you now check the library you should see the new, and updated, file.

You can check the ghosted/unghosted status of files by running:

$masterPageGallery = "Master Page Gallery"
$url = "http://www.example.com"

$site = Get-SPSite $url
$list = $site.RootWeb.Lists[$masterPageGallery]

foreach($item in $list.Items)
{      Write-Host $item.Name "-" $item.File.CustomizedPageStatus

If you find that the file you are trying to update is unghosted you got a few different options. You can add little bit of logic to the snippet above to check the status of the files and programmatically revert them to their ghosted state. I.e. instead of just printing them to the prompt, run $item.File.RevertContentStream(); to re-ghost the files you found to be unghosted.

Another option is to use some some external tool, like http://spghostfilesmanager.codeplex.com/,  to re-ghost the files you need ghosted.

For more information on SPFile, CustomizedPageStatus and RevertContentStream(), read http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spfile.aspx.

Also check out http://jamestsai.net/Blog/post/SPFileCustomizedPageStatus-amp3b-SPFileRevertContentStream-The-way-to-check-SPFile-GhostUn-Ghost-status-and-Revert-to-site-definition-via-code.aspx for more information on what makes a file unghosted and how you can programmatically re-ghost it.

1 comment: