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

2 thoughts on “Best Approach To Activate MasterPage for SharePoint Web”

  1. A few suggestions:
    1) You shouldn’t dispose properties.Feature.Parent.
    2) If a user has permissions to activate a feature, they will usually have permissions to change the master page – RWEP is redundant.
    3) AllowUnsafeUpdates is only required if the code will execute on a GET – activating through the web is a POST.
    4) Why use SPContext.Current when you already have web as a reference to the current site? This will break if the feature is activated from stsadm.
    5) When you’re checking for existence you might want to support master pages that are provisioned in site collection’s root master page gallery as well.
    6) I would probably move all the constants to feature properties so the receiver can be reused.

    Cheers ~
    Keith

Leave a Reply

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