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.
-
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
-
}
Following, the Constants class, as the name implies, defines constants!
-
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/";
-
}
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.
-
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<SPListItem>().
-
Where(item => Equals(item[Constants.PROPERTY_TO_QUERY].ToString(), templateName)).FirstOrDefault();
-
if (ensureExistance != null)
-
{
-
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;
-
}
-
});
-
}
-
}
I wonder if there is a more efficient way of achieving this same requirement.
Related posts:
- Static Versioning in a WebPart EditorPart
- Auto-Implemented Properties and Developing WebParts
- ToolPart Class With System.Reflection
- Protected Constructors For Abstract Classes
- Building SharePoint View Queries with CAML
2 Comments »
RSS feed for comments on this post. TrackBack URL























Articles & Research
SharePoint Architecture
Research Methodology
Personal/Off-Topic
Article Or Research Filed Under 

[...] Best Approach To Activate MasterPage for SharePoint Web [...]
Pingback by Links (9/27/2009 « Steve Pietrek – Everything SharePoint) — September 27, 2009 @ 5:23 pm
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
Comment by Keith Dahlby — September 28, 2009 @ 8:15 am