So, you are deploying branding through Visual Studio into SharePoint 2010, which includes your Masterpage(s) and Page Layout(s).
It works great the first time doesn’t it? But what then . . . as soon as you use the page layout in any of your sites, it gets locked down and can no longer be deleted and replaced.
The answer:
- CKSDEV tools and this nifty little script below. If you have never used CKSDEV tools, DO IT . . . it’s an amazing plugin for Visual Studio 2010 that will help you love SharePoint deployment.
- Set your project to Upgrade instead of the normal Default on deployment (screenshot below).

- Ghost your files after upgrade/deployment (in SharePoint Designer, this is called Reset to Site Definition) with the code below.
In your Event Receiver under FeatureActivated, add the following code.
using (SPSite siteCollection = (SPSite)properties.Feature.Parent)
{
if (siteCollection != null)
{
// enumerate through each site and remove custom branding
foreach (SPWeb site in siteCollection.AllWebs)
{
SPFile pageLayout = site.GetFile(site.Url + "/_catalogs/masterpage/WebpartPage.aspx");
if (pageLayout.Exists && pageLayout.CustomizedPageStatus == SPCustomizedPageStatus.Customized)
{
pageLayout.RevertContentStream();
}
}
}
}
Some important things to note about this code:
- the using statement avoids memory leaks
- always check if SPAnything is null before using it – SharePoint will toss its cookies on quite a few controls if it has a null reference.
- foreach SPWeb will open every website under the current site collection, which allows you to target all sites where the masterpages and page layouts are deployed
- check if the SPFile pageLayout is SPCustomizedPageStatus.Customized
- RevertContentStream resets the file to site definition.
Sweet… Awesome… Deliciousness… Hope this helps!
