Задайте вопросЗадайте вопрос
 

ОтвеченоSetting custom masterpage doesn't work for new subsites

  • 16 июня 2009 г. 13:35bs_stuff Медали пользователяМедали пользователяМедали пользователяМедали пользователяМедали пользователя
     С кодом
    Hi,

    I've created a feature that when activated, automatically aplies my custom masterpage to all sites and subsites.  What it fails to do is apply the custom masterpage to new subsites that are created after the feature has been activated.  I need this to be able to happen automatically, and without feature stapling...which isn't supported in our environment.  Please see the code below and let me know if it can be improved to handle new subsites.  Thanks in advance!
    using System;
    using System.Collections;
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Administration;
    using Microsoft.SharePoint.Publishing;
    
    
    
    namespace CustomDisplayLevels
    {
    
        public class FeatureReceiver : SPFeatureReceiver
        {
            const string defaultMasterUrl = "/_catalogs/masterpage/default.master";
            const string customizedMasterUrl = "/_catalogs/masterpage/CustomDisplayLevels.master";
    
            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;
    
                string relativeURL = rootWeb.ServerRelativeUrl;
                if (relativeURL == "/")
                    relativeURL = "";
    
                rootWeb.AllProperties["OldMasterUrl"] = rootWeb.MasterUrl;
                rootWeb.AllProperties["OldCustomMasterUrl"] = rootWeb.CustomMasterUrl;
                rootWeb.MasterUrl = relativeURL + customizedMasterUrl;
                rootWeb.CustomMasterUrl = relativeURL + customizedMasterUrl;
                rootWeb.Update();
    
                foreach (SPWeb subWeb in rootWeb.Webs)
                {
                    ProcessSubWebs(subWeb, true);
                }
            }
    
            private void ProcessSubWebs(SPWeb web, bool isActivation)
            {
                if (isActivation)
                {
                    PublishingWeb pweb = PublishingWeb.GetPublishingWeb(web);
                    pweb.MasterUrl.SetInherit(true, true);
                    pweb.CustomMasterUrl.SetInherit(true, true);
                    
                }
                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();
    
                string relativeURL = rootWeb.ServerRelativeUrl;
                if (relativeURL == "/")
                    relativeURL = "";
    
                foreach (SPWeb subWeb in rootWeb.Webs)
                {
                    ProcessSubWebs(subWeb, false);
                }
    
    
    
                if (rootWeb.MasterUrl != relativeURL + customizedMasterUrl)
                {
                    try
                    {
                        bool fileExists = rootWeb.GetFile(relativeURL + customizedMasterUrl).Exists;
                        SPFile file = rootWeb.GetFile(relativeURL + customizedMasterUrl);
                        SPFolder masterPageGallery = file.ParentFolder;
    
                        SPFolder temp = masterPageGallery.SubFolders.Add("Temp");
                        file.MoveTo(temp.Url + "/" + file.Name);
                        temp.Delete();
    
                    }
                    catch (ArgumentException)
                    {
                        return;
                    }
                }
            }
    
            private void DeactivateWeb(SPWeb web)
            {
                string relativeURL = web.ServerRelativeUrl;
                if (relativeURL == "/")
                    relativeURL = "";
                
                if (web.AllProperties.ContainsKey("OldMasterUrl"))
                {
                    string oldMasterUrl = web.AllProperties["OldMasterUrl"].ToString();
                    try
                    {
                        bool fileExists = web.GetFile(oldMasterUrl).Exists;
                        web.MasterUrl = oldMasterUrl;
                    }
                    catch (ArgumentException)
                    {
                        web.MasterUrl = relativeURL + defaultMasterUrl;
                    }
    
                    string oldCustomUrl = web.AllProperties["OldCustomMasterUrl"].ToString();
                    try
                    {
                        bool fileExists = web.GetFile(oldCustomUrl).Exists;
                        web.CustomMasterUrl = web.AllProperties["OldCustomMasterUrl"].ToString();
                    }
                    catch (ArgumentException)
                    {
                        web.CustomMasterUrl = relativeURL + defaultMasterUrl;
                    }
    
                    web.AllProperties.Remove("OldMasterUrl");
                    web.AllProperties.Remove("OldCustomMasterUrl");
                }
                else
                {
                    web.MasterUrl = relativeURL + defaultMasterUrl;
                    web.CustomMasterUrl = relativeURL + defaultMasterUrl;
                }
            }
        }
    }
    

Ответы

Все ответы