However, Microsoft warns us NOT to
edit the "default.master" as it will
be overwritten by the next service
pack or patch.
Copy & Paste new masterpage with different name and use it as the default one. Use either SharePoint designer or programmatically set SPWeb.MasterUrl and/or SPWeb.CustomMasterPage.
For this, i have 2 features
- One to set custom master page on current website
- Other one to activate previous feature on all webs + staple feature to newly created web's
(MWSBalticovo is for meeting workspace - they have a different masterpage)
Web scoped feature for a single web site to use custom masterpage.
I have a feature with my custom master page packaged:
<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<Module Name="BalticovoMasterPages" List="116" Url="_catalogs/masterpage" RootWebOnly="TRUE" Path="MasterPages">
<File Url="Balticovo.master" Type="GhostableInLibrary" IgnoreIfAlreadyExists="TRUE">
<Property Name="ContentType" Value="$Resources:core,MasterPage;"/>
<Property Name="MasterPageDescription" Value="$Resources:Balticovo,BalticovoMasterPageDescription;"/>
</File>
<File Url="MWSBalticovo.master" Type="GhostableInLibrary" IgnoreIfAlreadyExists="TRUE">
<Property Name="ContentType" Value="$Resources:core,MasterPage;"/>
<Property Name="MasterPageDescription" Value="$Resources:Balticovo,MWSBalticovoMasterPageDescription;"/>
</File>
</Module>
</Elements>
And FeatureReceiver:
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPWeb web = properties.Feature.Parent as SPWeb;
string masterUrl = "/_catalogs/masterpage/Balticovo.master";
string mwsMasterUrl = "/_catalogs/masterpage/MWSBalticovo.master";
if (web.CustomMasterUrl.ToLower().Contains("/mws")) //meeting workspace
web.CustomMasterUrl = mwsMasterUrl;
else
web.CustomMasterUrl = masterUrl;
web.MasterUrl = masterUrl;
web.Update();
}
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
SPWeb web = properties.Feature.Parent as SPWeb;
web.MasterUrl = "/_catalogs/masterpage/default.master";
if (web.CustomMasterUrl.ToLower().Contains("/mws")) //meetng workspace
web.CustomMasterUrl = "/_catalogs/masterpage/MWSdefault.master";
else
web.CustomMasterUrl = "/_catalogs/masterpage/default.master";
web.Update();
}
2nd Site scoped feature to enable previous features
elements.xml (activate 1st feature on newly created web's, but will not activate on existing):
<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<FeatureSiteTemplateAssociation
TemplateName="GLOBAL"
Id="{227c6aed-f66b-482d-aea8-a2af3ca203b7}" />
</Elements>
FeatureReceiver (activate 1st feature on existing webs):
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
Guid masterPageFeatureId = new Guid("{227c6aed-f66b-482d-aea8-a2af3ca203b7}");
SPSite site = properties.Feature.Parent as SPSite;
SPWebCollection webs = site.AllWebs;
foreach (SPWeb web in webs)
{
try
{
if (web.Features[masterPageFeatureId] == null)
web.Features.Add(masterPageFeatureId);
}
catch (InvalidOperationException) //target feature not yet installed
{ throw; }
catch (SPException) { } //If feature could not be activated.
finally
{
if (web != null)
web.Dispose();
}
}
}
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
Guid masterPageFeatureId = new Guid("{227c6aed-f66b-482d-aea8-a2af3ca203b7}");
SPSite site = properties.Feature.Parent as SPSite;
SPWebCollection webs = site.AllWebs;
foreach (SPWeb web in webs)
{
try
{
if (web.Features[masterPageFeatureId] == null)
web.Features.Remove(masterPageFeatureId);
}
catch (InvalidOperationException) { }
catch (SPException) { }
finally
{
if (web != null)
web.Dispose();
}
}
}