Pour les professionnels de l’informatique > Forums - Accueil > SharePoint - Development and Programming > Setting custom masterpage doesn't work for new subsites
Poser une questionPoser une question
 

TraitéeSetting custom masterpage doesn't work for new subsites

  • mardi 16 juin 2009 13:35bs_stuff Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     A du code
    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;
                }
            }
        }
    }
    

Réponses

Toutes les réponses

  • mardi 16 juin 2009 13:42Dave Hunter Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    Why is feature stapling not supported in your environment?  You cannot capture the event of a site being created without using feature stapling.
    My SharePoint Blog - http://www.davehunter.co.uk/blog
  • mardi 16 juin 2009 14:21bs_stuff Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    I just had a discussion and found that actually, feature stapling to out of the box site definitions is not allowed, but stapling to custom site definitions IS allowed!  Can someone help me with what I need to do in addition to what I already have?

    Thanks!
  • mardi 16 juin 2009 18:25bs_stuff Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    To clarify, I'm not sure how to create then incorporate a custom site definition and stapled feature with the feature I already have.  I can only say that it must be deployed as a wsp...I don't have access to physically get on the server and copy site definitions/folders/etc...as I've seen it done in a few examples.

    Any ideas?
  • jeudi 18 juin 2009 20:55Dave Hunter Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     Traitée
    Have a look at these posts for creating a custom site definition http://www.sharepointblogs.com/tbaginski/archive/2007/08/16/creating-a-custom-site-definition-in-wss-v3-moss.aspx and http://msdn.microsoft.com/en-us/library/ms454677.aspx

    You then need to create a feature which contains a FeatureSiteTemplateAssociation http://msdn.microsoft.com/en-us/library/aa544552.aspx.  More about feature stapling http://www.sharepointdevwiki.com/display/public/FeatureSiteTemplateAssociation

    Hope this helps

    Dave
    My SharePoint Blog - http://www.davehunter.co.uk/blog
  • vendredi 3 juillet 2009 16:11CodeHawk Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     Réponse proposée

    Hi,
    In general, feature stapling is not necessary for new subsites to inherit a custom master page.  The master page for the top level site can be pushed down to subsites. 

    1. Open the top level site in a browser.  On the Site Actions menu, point to Site Settings, and then click Modify All Site Settings. 
    2. Click Master Page under the Look and Feel group.
    3. Select your master page in the drop down list for both the Site Master Page and System Master Page. Check the box to apply the master page to all sub-sites. Click OK.

    If there are errors:
    1. Ensure the master page is part of the Master Page Gallery:
    Open the top level site in a browser. On the Site Actions menu, point to Site Settings, and then click Modify All Site Settings.
    Under the Galleries heading, select Master Pages. The Master Page Gallery loads. If you can't find your master page, upload it using the Upload feature in the library toolbar.

    2. Ensure the master page is published and approved:
    Open the top level site in a browser. On the Site Actions menu, point to Site Settings, and then click Modify All Site Settings.
    Under the Galleries heading, select Master Pages. The Master Page Gallery loads. If your master page is not listed as approved it must be published and approved.
    Using the drop-down menu associated with the master page file name, select Publish.
    After publishing, use the drop-down menu again to select Approve.

    3. Review  http://support.microsoft.com/default.aspx/kb/936908


    MCSD (VS 6)
    • Proposé comme réponseCodeHawk lundi 6 juillet 2009 19:22
    •  
  • vendredi 3 juillet 2009 16:17Dave Hunter Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    CodeHawk - This is a manual process bs_stuff wanted this to be automatically. Feature stapling is required if you want this to be set automatically.
    My SharePoint Blog - http://www.davehunter.co.uk/blog
  • vendredi 3 juillet 2009 17:03CodeHawk Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     

    Yes I did refer to the manual process.  By default a newly created subsite always inherits its master page from its parent site so, in general, feature stapling is not required.  Additionally bs_stuff notes difficulty utilizing feature stapling.  It seems reasonable to at least attempt the manual process, which should work by design.

    Just a few references:

    Beginning SharePoint 2007 By Amanda Murphy, Shane Perran

    SharePoint 2007 User's Guide By Seth Bates, Tony Smith

    Microsoft® Office SharePoint® Server 2007 By David Matthew Sterling

    PROFESSIONAL SHAREPOINT 2007 DESIGN By Jacob J. Sanford, Randy Drisgill, David Drinkwine, Coskun Cavusoglu, Heather Solomon

    Beginning SharePoint 2007 Administration By Göran Husman


    Happy Coding, ~CodeHawk MCSD | MCTS [SharePoint Development]
  • jeudi 6 août 2009 09:04Tony_is_here Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    Hi CodeHawk,
    I dont think you understand how SharePoint works. If you create a MasterPage feature and apply it to a site, new subsites created after this do not inherit the masterpage. Feature stapling is required to automatically turn apply the feature created masterpage to the subsite otherwise you have to do it manually everytime you create a subsite.
    This is for custom masterpages.

  • jeudi 6 août 2009 12:25CodeHawk Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     

    Hi Tony_is_here,
    Perhaps you didn't review the references I posted.

    To clarify: 
    If the situation is to apply the same custom master page from the top level site down when new subsites are created than a feature via feature stapling is NOT needed.  Yes, setting the master page for the top level site is a manual process -- all of 30 seconds.

    If the situation is to apply a custom master page that is DIFFERENT from the top level site down when all new subsites are created then a feature via feature stapling is useful.

    Too often fellow developers are quick to add additional complexity because they lack the knowledge of SharePoint 2007's inherent abilities. 

    Best Regards


    Happy Coding, ~CodeHawk MCSD | MCTS [SharePoint Development]
  • jeudi 6 août 2009 13:04Tony_is_here Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    Most companies will have a number of masterpages for different departments and if you staple the feature to a template then creating a new site will have the correct master page applied to it. Otherwise you have to go to the top site in the site collection and click the inherit box each time a new site is created. This is obviously not the best approach if you have a dynamic company which is constantly creating sites and workspaces.

    •  
  • vendredi 7 août 2009 08:04Tony_is_here Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     

    A Portal can host a number of different sites representing different departments/schools/companies. If each of these have different site collections then you may need to create a feature to deploy a master page. To automatically create a new subsite that inherits a custom master page then you need to feature staple it to the template.