In all the SharePoint projects I've work on, which included custom masterpages or page layouts. I've always made sure these provisioned files started with some sort of prefix.
Like this public website for instance:
Advantive.Internet.master
Advantive.BlogPostA.aspx
Advantive.StartPage.aspx
and so on, and so on...
But when installing a new version af the solution, it sometimes happened the new versions of the files were not used by SharePoint and we had to re-ghost all files.
This can be done with SharePoint designer of from the site settigs page.
After all these years I've been working with SharePoint, I never took the time to write a decent command line tool or STSADM extention to help me with this problem.
Just so you could easily say: revert all files in the masterpage galleries of all site collections and sub sites in web application X that have a filename starting with prefix Y.
And it could be so easy to create, because the SPFile object contains a method called RevertToStream() that does the job.
Well, during one of my latest SharePoint 2010 projects, it was time to create some sort of thingy that would be able to do just that. And this powershell script was born:
Param([Parameter(Mandatory = $true)][System.String]${url},
[Parameter(Mandatory = $true)][System.String]${prefix})
Start-SPAssignment -Global
function ReghostFiles($web)
{
Write-Host "Processing web:" $web.ServerRelativeUrl
$folderMP = $web.GetFolder('_catalogs/masterpage')
if ($folderMP.Exists)
{
$count = 0
Write-Host "Found masterpage gallery with" $folderMP.Files.Count "files."
foreach ($file in $folderMP.Files)
{
if ($file.Name.StartsWith($prefix))
{
Write-Host "Re-ghosting file:" $file.name
$file.RevertContentStream()
$count++
}
}
Write-Host "Re-ghosted $count files."
}
foreach ($subWeb in $web.Webs)
{
ReghostFiles $subWeb
$subWeb.Dispose()
}
}
if ($url -ne $null)
{
if ($url.EndsWith("/") -eq $false)
{
$url = $url + "/"
}
Write-Host "Getting web application with url:" $url
$webApp = Get-SPWebApplication | where { $_.url -eq $url }
if ($webApp -ne $null)
{
$name = $webApp.Name
$siteCount = $webApp.Sites.Count
Write-Host "Found web application:" $name
Write-Host "Containing $siteCount site collection(s)..."
if ($siteCount -ne 0)
{
foreach ($site in $webApp.Sites)
{
Write-Host "Processing webs in site collection:" $site.Url
ReghostFiles $site.RootWeb
$site.Dispose()
}
}
}
else
{
Write-Host "Web application not found!"
}
}
Stop-SPAssignment -Global
Calling the script from a SharePoint 2010 management console, it requires 2 parameters: the first one is the url of the web application en the second one is the prefix of the files to re-ghost.
All webs in all site collections in the web application are processed.
I hope it will come in handy to you to!