Best Approach To Activate MasterPage for SharePoint Web

Going through a quarterly code review with a company today, we started to talk about the best way that a masterpage should be programmatically activated for a particular SharePoint web as the way being used by some components was fairly poor.

Below is what we decided on, which I don’t know how I feel about. I am leaving a lot of the exception handling out for brevity.

Firstly, the GenericMasterPage super class inherits from the SPFeatureReceiver concrete class, implementing all the obligatory abstract inherited members. This just contains references to the static application methods in the Builders class.

[csharp]
public class GenericMasterPage : SPFeatureReceiver
{
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
Builders.IterateGalleryAndApplyTemplate(properties, Constants.GENERIC_MASTER_PAGE_NAME);
}
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
Builders.IterateGalleryAndApplyTemplate(properties, Constants.DEFAULT_MASTER_PAGE_NAME);
}
#region Unused Abstract Inherited Members
public override void FeatureInstalled(SPFeatureReceiverProperties properties)
{
// This abstract inherited member requires no behavior modification
}

public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
{
// This abstract inherited member requires no behavior modification
}
#endregion
}
[/csharp]

Following, the Constants class, as the name implies, defines constants!

[csharp]
public class Constants
{
public const string DEFAULT_MASTER_PAGE_NAME = “default.master”;
public const string GALLERY_LIBRARY_NAME = “Master Page Gallery”;
public const string GENERIC_MASTER_PAGE_NAME = “lps.master”;
public const string PROPERTY_TO_QUERY = “Name”;
public const string MASTER_REL_LINK = “_catalogs/masterpage/”;
}
[/csharp]

Lastly, the Builders class contains the static IterateGalleryAndApplyTemplate method which will use a LINQ query to ensure that the master page to apply is available, and then apply it to the current SharePoint web.

[csharp]
public class Builders
{
public static void IterateGalleryAndApplyTemplate(SPFeatureReceiverProperties properties, string templateName)
{
SPSecurity.RunWithElevatedPrivileges(() =>
{
using (var web = (SPWeb) properties.Feature.Parent)
{
web.AllowUnsafeUpdates = true;
var ensureExistance = SPContext.Current.Web.Lists[Constants.GALLERY_LIBRARY_NAME].Items.Cast().
Where(item => Equals(item[Constants.PROPERTY_TO_QUERY].ToString(), templateName)).FirstOrDefault();
if (ensureExistance != null)
{
string masterUrl = new Uri(web.Url).GetComponents(UriComponents.Path, UriFormat.Unescaped);
if (!masterUrl.StartsWith(“/”)) masterUrl = String.Format(“/{0}”, masterUrl);
if (!masterUrl.EndsWith(“/”)) masterUrl += “/”;
masterUrl += Constants.MASTER_REL_LINK + templateName;
web.MasterUrl = masterUrl;
web.CustomMasterUrl = masterUrl;
}
web.Update();
web.AllowUnsafeUpdates = false;
}
});
}
}
[/csharp]

I wonder if there is a more efficient way of achieving this same requirement.

Share