Feature Receiver to apply master pages to normal or meeting sites

As noted previously, Meeting Workspaces use a different master page to the ‘normal’ master pages in SharePoint. This is a little annoying – if I need to apply a new master page, how would I do this?

Well, you can activate a new master page in a Feature Receiver. And we can detect the type of site we’re dealing with. Why not combine the two techniques:

private const string MASTERPAGEFILE = "MasterPageFile";
private const string MEETINGMASTERPAGEFILE = "MeetingMasterPageFile";

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPWeb site = properties.Feature.Parent as SPWeb;
SPFeatureProperty masterFile = properties.Definition.Properties[MASTERPAGEFILE];
SPFeatureProperty meetingMasterFile = properties.Definition.Properties[MEETINGMASTERPAGEFILE];
char[] slashes = { '/' };
//Is the site a Meeting Workspace?
if (site.WebTemplate.StartsWith("MPS"))
{
site.CustomMasterUrl = site.ServerRelativeUrl.TrimEnd(slashes) + "/_catalogs/masterpage/" + meetingMasterFile.Value;
site.MasterUrl = site.ServerRelativeUrl.TrimEnd(slashes) + "/_catalogs/masterpage/" + masterFile.Value;
}
else
{
site.CustomMasterUrl = site.ServerRelativeUrl.TrimEnd(slashes) + "/_catalogs/masterpage/" + masterFile.Value;
site.MasterUrl = site.ServerRelativeUrl.TrimEnd(slashes) + "/_catalogs/masterpage/" + masterFile.Value;
}
site.Update();
}

3 thoughts on “Feature Receiver to apply master pages to normal or meeting sites

  1. Interesting,
    now it is easy to change master page programmaticly
    Thanks for writing, most people don’t bother.

  2. Thanks for sharing.

    One note though, instead of testing template name like this

    if (site.WebTemplate.StartsWith(“MPS”))

    I would recommend using

    if (SPMeeting.IsMeetingWorkspaceWeb(site))

    Regards

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>