Geeks With Blogs
Corey VanDyke's Blog SharePoint & Microsoft Technologies
| Home |

Some of you may have read a blog post by the same name as above.  It was written by Becky Bertram.  It is a great blog, but I ran into a couple of issues that I know many of you have as well.  The main issue I found was that on sub site collections we were getting "File Not Found" error's.  No one ever got back to me on a solution, so I decided to modify the featurereceiver.cs file just a bit to solve the problem.  You will notice a few other fixes as well.  Other than that, I thank Becky for getting me a head start.  Below you will find the code for the feature receiver.

featurereceiver.cs

using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace GlobalMasterPage {
 
 public class FeatureReceiver : SPFeatureReceiver
 {
        const string defaultMasterUrl = "/_catalogs/masterpage/default.master";
        const string customizedMasterUrl = "/_catalogs/masterpage/MyCustom.master";
        private string defaultMaster;
        private string customMaster;

  public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {

        }

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
           
        }
  
  public override void FeatureActivated(SPFeatureReceiverProperties properties)
  {
   SPSite site = properties.Feature.Parent as SPSite;
           

   if (site == null)
    return;

            SPWeb rootWeb = site.RootWeb;
            rootWeb.AllProperties["OldMasterUrl"] = rootWeb.MasterUrl;
            rootWeb.AllProperties["OldCustomMasterUrl"] = rootWeb.CustomMasterUrl;


            customMaster = (rootWeb.ServerRelativeUrl.Length == 1 ? "" : rootWeb.ServerRelativeUrl) + customizedMasterUrl;
                defaultMaster = defaultMasterUrl;
                rootWeb.MasterUrl = customMaster;
                rootWeb.CustomMasterUrl = customMaster;
            rootWeb.Update();

            foreach (SPWeb subWeb in rootWeb.Webs)
            {
                ProcessSubWebs(subWeb, true);
            }

  }
  
  private void ProcessSubWebs(SPWeb web, bool isActivation)
  {
   if (isActivation)
   {
                web.AllProperties["OldMasterUrl"] = web.MasterUrl;
                web.AllProperties["OldCustomMasterUrl"] = web.CustomMasterUrl;

                if (web.ServerRelativeUrl.Length > 1)
                {
                    customMaster = (web.Site.RootWeb.ServerRelativeUrl.Length == 1 ? "" : web.Site.RootWeb.ServerRelativeUrl) + customizedMasterUrl;
                    web.MasterUrl = customMaster;
                    web.CustomMasterUrl = customMaster;
                }

               
   }
   else
   {
    DeactivateWeb(web);
   }
   web.Update();
   
   foreach (SPWeb subWeb in web.Webs)
   {
    ProcessSubWebs(subWeb, isActivation);
   }
  }
  
  public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
  {
   SPSite site = properties.Feature.Parent as SPSite;
   if (site == null)
    return;
  
   SPWeb rootWeb = site.RootWeb;
   DeactivateWeb(rootWeb);
   rootWeb.Update();
   
   foreach (SPWeb subWeb in rootWeb.Webs)
   {
    ProcessSubWebs(subWeb, false);
   }

   if (rootWeb.MasterUrl != customMaster)
   {
    try
    {
     bool fileExists = rootWeb.GetFile(customMaster).Exists;
     SPFile file = rootWeb.GetFile(customMaster);
     SPFolder masterPageGallery = file.ParentFolder;
     
    }
    catch (ArgumentException)
    {
     return;
    }
   }
  }

  private void DeactivateWeb(SPWeb web) {
   if (web.AllProperties.ContainsKey("OldMasterUrl")) {
    string oldMasterUrl = web.AllProperties["OldMasterUrl"].ToString();
    try {
     bool fileExists = web.GetFile(oldMasterUrl).Exists;
     web.MasterUrl = oldMasterUrl;
    }
    catch (ArgumentException) {
                    defaultMaster = (web.Site.RootWeb.ServerRelativeUrl.Length == 1 ? "" : web.Site.RootWeb.ServerRelativeUrl) + defaultMasterUrl;
                    web.MasterUrl = defaultMaster;
    }

    string oldCustomUrl = web.AllProperties["OldCustomMasterUrl"].ToString();
    try {
     bool fileExists = web.GetFile(oldCustomUrl).Exists;
     web.CustomMasterUrl = web.AllProperties["OldCustomMasterUrl"].ToString();
    }
    catch (ArgumentException) {
                    defaultMaster = (web.Site.RootWeb.ServerRelativeUrl.Length == 1 ? "" : web.Site.RootWeb.ServerRelativeUrl) + defaultMasterUrl;
                    web.CustomMasterUrl = defaultMaster;
    }

    web.AllProperties.Remove("OldMasterUrl");
    web.AllProperties.Remove("OldCustomMasterUrl");
   }
   else {
                //web.MasterUrl = defaultMasterUrl;
                //web.CustomMasterUrl = defaultMasterUrl;

                defaultMaster = (web.Site.RootWeb.ServerRelativeUrl.Length == 1 ? "" : web.Site.RootWeb.ServerRelativeUrl) + defaultMasterUrl;
                web.MasterUrl = defaultMaster;
                web.CustomMasterUrl = defaultMaster;
   }
            //if (web.ServerRelativeUrl.Length > 1)
            //{
            //    defaultMaster = web.Site.RootWeb.ServerRelativeUrl + defaultMasterUrl;
            //    web.MasterUrl = defaultMaster;
            //    web.CustomMasterUrl = defaultMaster;
            //}
  }
 }
}

Posted on Thursday, April 10, 2008 10:34 AM SharePoint | Back to top


Comments on this post: Applying a SharePoint Custom Master Page Globally

# re: Applying a SharePoint Custom Master Page Globally
Requesting Gravatar...
Thank you so much. Just like you said, Beckys example didnt work for me, I got the error you mentioned and I couldnt understand why. This solved it!

Thanks again.
Left by Emelie on May 14, 2008 9:08 AM

Your comment:
 (will show your gravatar)
 


Copyright © cvandjk | Powered by: GeeksWithBlogs.net | Join free