kamagra how much to take

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.

  1. public class GenericMasterPage : SPFeatureReceiver
  2. {
  3. public override void FeatureActivated(SPFeatureReceiverProperties properties)
  4. {
  5. Builders.IterateGalleryAndApplyTemplate(properties, Constants.GENERIC_MASTER_PAGE_NAME);
  6. }
  7. public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
  8. {
  9. Builders.IterateGalleryAndApplyTemplate(properties, Constants.DEFAULT_MASTER_PAGE_NAME);
  10. }
  11. #region Unused Abstract Inherited Members
  12. public override void FeatureInstalled(SPFeatureReceiverProperties properties)
  13. {
  14. // This abstract inherited member requires no behavior modification
  15. }
  17. public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
  18. {
  19. // This abstract inherited member requires no behavior modification
  20. }
  21. #endregion
  22. }

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

  1. public class Constants
  2. {
  3. public const string DEFAULT_MASTER_PAGE_NAME = "default.master";
  4. public const string GALLERY_LIBRARY_NAME = "Master Page Gallery";
  5. public const string GENERIC_MASTER_PAGE_NAME = "lps.master";
  6. public const string PROPERTY_TO_QUERY = "Name";
  7. public const string MASTER_REL_LINK = "_catalogs/masterpage/";
  8. }

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.

  1. public class Builders
  2. {
  3. public static void IterateGalleryAndApplyTemplate(SPFeatureReceiverProperties properties, string templateName)
  4. {
  5. SPSecurity.RunWithElevatedPrivileges(() =>
  6. {
  7. using (var web = (SPWeb) properties.Feature.Parent)
  8. {
  9. web.AllowUnsafeUpdates = true;
  10. var ensureExistance = SPContext.Current.Web.Lists[Constants.GALLERY_LIBRARY_NAME].Items.Cast<SPListItem>().
  11. Where(item => Equals(item[Constants.PROPERTY_TO_QUERY].ToString(), templateName)).FirstOrDefault();
  12. if (ensureExistance != null)
  13. {
  14. string masterUrl = new Uri(web.Url).GetComponents(UriComponents.Path, UriFormat.Unescaped);
  15. if (!masterUrl.StartsWith("/")) masterUrl = String.Format("/{0}", masterUrl);
  16. if (!masterUrl.EndsWith("/")) masterUrl += "/";
  17. masterUrl += Constants.MASTER_REL_LINK + templateName;
  18. web.MasterUrl = masterUrl;
  19. web.CustomMasterUrl = masterUrl;
  20. }
  21. web.Update();
  22. web.AllowUnsafeUpdates = false;
  23. }
  24. });
  25. }
  26. }

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


One Comment

  1. Keith Dahlby says:

    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 ~


  1. Links (9/27/2009) « Steve Pietrek – Everything SharePoint - [...] Best Approach To Activate MasterPage for SharePoint Web [...]

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=""> <s> <strike> <strong>