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. }
  16.  
  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.

Share

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>