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();
}

Feature Receiver to apply master pages to normal or meeting sites

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

  1. DizTortion says:

    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 to geeks Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.