Setting custom masterpage doesn't work for new subsitesHi,<br/><br/>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! <pre lang="x-c#">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 = &quot;/_catalogs/masterpage/default.master&quot;; const string customizedMasterUrl = &quot;/_catalogs/masterpage/CustomDisplayLevels.master&quot;; 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 == &quot;/&quot;) relativeURL = &quot;&quot;; rootWeb.AllProperties[&quot;OldMasterUrl&quot;] = rootWeb.MasterUrl; rootWeb.AllProperties[&quot;OldCustomMasterUrl&quot;] = 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 == &quot;/&quot;) relativeURL = &quot;&quot;; 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(&quot;Temp&quot;); file.MoveTo(temp.Url + &quot;/&quot; + file.Name); temp.Delete(); } catch (ArgumentException) { return; } } } private void DeactivateWeb(SPWeb web) { string relativeURL = web.ServerRelativeUrl; if (relativeURL == &quot;/&quot;) relativeURL = &quot;&quot;; if (web.AllProperties.ContainsKey(&quot;OldMasterUrl&quot;)) { string oldMasterUrl = web.AllProperties[&quot;OldMasterUrl&quot;].ToString(); try { bool fileExists = web.GetFile(oldMasterUrl).Exists; web.MasterUrl = oldMasterUrl; } catch (ArgumentException) { web.MasterUrl = relativeURL + defaultMasterUrl; } string oldCustomUrl = web.AllProperties[&quot;OldCustomMasterUrl&quot;].ToString(); try { bool fileExists = web.GetFile(oldCustomUrl).Exists; web.CustomMasterUrl = web.AllProperties[&quot;OldCustomMasterUrl&quot;].ToString(); } catch (ArgumentException) { web.CustomMasterUrl = relativeURL + defaultMasterUrl; } web.AllProperties.Remove(&quot;OldMasterUrl&quot;); web.AllProperties.Remove(&quot;OldCustomMasterUrl&quot;); } else { web.MasterUrl = relativeURL + defaultMasterUrl; web.CustomMasterUrl = relativeURL + defaultMasterUrl; } } } }</pre>© 2009 Microsoft Corporation. All rights reserved.Fri, 07 Aug 2009 08:04:43 Z31e038a0-cf89-4e7c-bcee-9bd1ddaf9635http://social.technet.microsoft.com/Forums/en-US/sharepointdevelopment/thread/31e038a0-cf89-4e7c-bcee-9bd1ddaf9635#31e038a0-cf89-4e7c-bcee-9bd1ddaf9635http://social.technet.microsoft.com/Forums/en-US/sharepointdevelopment/thread/31e038a0-cf89-4e7c-bcee-9bd1ddaf9635#31e038a0-cf89-4e7c-bcee-9bd1ddaf9635bs_stuffhttp://social.technet.microsoft.com/Profile/en-US/?user=bs_stuffSetting custom masterpage doesn't work for new subsitesHi,<br/><br/>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! <pre lang="x-c#">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 = &quot;/_catalogs/masterpage/default.master&quot;; const string customizedMasterUrl = &quot;/_catalogs/masterpage/CustomDisplayLevels.master&quot;; 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 == &quot;/&quot;) relativeURL = &quot;&quot;; rootWeb.AllProperties[&quot;OldMasterUrl&quot;] = rootWeb.MasterUrl; rootWeb.AllProperties[&quot;OldCustomMasterUrl&quot;] = 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 == &quot;/&quot;) relativeURL = &quot;&quot;; 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(&quot;Temp&quot;); file.MoveTo(temp.Url + &quot;/&quot; + file.Name); temp.Delete(); } catch (ArgumentException) { return; } } } private void DeactivateWeb(SPWeb web) { string relativeURL = web.ServerRelativeUrl; if (relativeURL == &quot;/&quot;) relativeURL = &quot;&quot;; if (web.AllProperties.ContainsKey(&quot;OldMasterUrl&quot;)) { string oldMasterUrl = web.AllProperties[&quot;OldMasterUrl&quot;].ToString(); try { bool fileExists = web.GetFile(oldMasterUrl).Exists; web.MasterUrl = oldMasterUrl; } catch (ArgumentException) { web.MasterUrl = relativeURL + defaultMasterUrl; } string oldCustomUrl = web.AllProperties[&quot;OldCustomMasterUrl&quot;].ToString(); try { bool fileExists = web.GetFile(oldCustomUrl).Exists; web.CustomMasterUrl = web.AllProperties[&quot;OldCustomMasterUrl&quot;].ToString(); } catch (ArgumentException) { web.CustomMasterUrl = relativeURL + defaultMasterUrl; } web.AllProperties.Remove(&quot;OldMasterUrl&quot;); web.AllProperties.Remove(&quot;OldCustomMasterUrl&quot;); } else { web.MasterUrl = relativeURL + defaultMasterUrl; web.CustomMasterUrl = relativeURL + defaultMasterUrl; } } } }</pre>Tue, 16 Jun 2009 13:35:58 Z2009-06-16T13:35:58Zhttp://social.technet.microsoft.com/Forums/en-US/sharepointdevelopment/thread/31e038a0-cf89-4e7c-bcee-9bd1ddaf9635#5b5ddcd4-5061-4e5e-9257-4157f101202dhttp://social.technet.microsoft.com/Forums/en-US/sharepointdevelopment/thread/31e038a0-cf89-4e7c-bcee-9bd1ddaf9635#5b5ddcd4-5061-4e5e-9257-4157f101202dDave Hunterhttp://social.technet.microsoft.com/Profile/en-US/?user=Dave%20HunterSetting custom masterpage doesn't work for new subsitesWhy is feature stapling not supported in your environment?  You cannot capture the event of a site being created without using feature stapling.<hr class="sig">My SharePoint Blog - <a href="http://www.davehunter.co.uk/blog">http://www.davehunter.co.uk/blog</a>Tue, 16 Jun 2009 13:42:22 Z2009-06-16T13:42:22Zhttp://social.technet.microsoft.com/Forums/en-US/sharepointdevelopment/thread/31e038a0-cf89-4e7c-bcee-9bd1ddaf9635#da5694b0-0902-4b8a-a87f-9910d4f74b36http://social.technet.microsoft.com/Forums/en-US/sharepointdevelopment/thread/31e038a0-cf89-4e7c-bcee-9bd1ddaf9635#da5694b0-0902-4b8a-a87f-9910d4f74b36bs_stuffhttp://social.technet.microsoft.com/Profile/en-US/?user=bs_stuffSetting custom masterpage doesn't work for new subsitesI 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?<br/><br/>Thanks!Tue, 16 Jun 2009 14:21:49 Z2009-06-16T14:21:49Zhttp://social.technet.microsoft.com/Forums/en-US/sharepointdevelopment/thread/31e038a0-cf89-4e7c-bcee-9bd1ddaf9635#4da09524-f9bc-4d8d-a734-a6f14a6bf7c0http://social.technet.microsoft.com/Forums/en-US/sharepointdevelopment/thread/31e038a0-cf89-4e7c-bcee-9bd1ddaf9635#4da09524-f9bc-4d8d-a734-a6f14a6bf7c0bs_stuffhttp://social.technet.microsoft.com/Profile/en-US/?user=bs_stuffSetting custom masterpage doesn't work for new subsitesTo 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.<br/><br/>Any ideas?Tue, 16 Jun 2009 18:25:55 Z2009-06-16T18:25:55Zhttp://social.technet.microsoft.com/Forums/en-US/sharepointdevelopment/thread/31e038a0-cf89-4e7c-bcee-9bd1ddaf9635#2684afd1-e682-4028-80d5-d4bc592ac842http://social.technet.microsoft.com/Forums/en-US/sharepointdevelopment/thread/31e038a0-cf89-4e7c-bcee-9bd1ddaf9635#2684afd1-e682-4028-80d5-d4bc592ac842Dave Hunterhttp://social.technet.microsoft.com/Profile/en-US/?user=Dave%20HunterSetting custom masterpage doesn't work for new subsitesHave a look at these posts for creating a custom site definition <a href="http://www.sharepointblogs.com/tbaginski/archive/2007/08/16/creating-a-custom-site-definition-in-wss-v3-moss.aspx">http://www.sharepointblogs.com/tbaginski/archive/2007/08/16/creating-a-custom-site-definition-in-wss-v3-moss.aspx</a> and <a href="http://msdn.microsoft.com/en-us/library/ms454677.aspx">http://msdn.microsoft.com/en-us/library/ms454677.aspx</a><br/><br/>You then need to create a feature which contains a FeatureSiteTemplateAssociation <a href="http://msdn.microsoft.com/en-us/library/aa544552.aspx">http://msdn.microsoft.com/en-us/library/aa544552.aspx</a>.  More about feature stapling <a href="http://www.sharepointdevwiki.com/display/public/FeatureSiteTemplateAssociation">http://www.sharepointdevwiki.com/display/public/FeatureSiteTemplateAssociation</a><br/><br/>Hope this helps<br/><br/>Dave<hr class="sig">My SharePoint Blog - <a href="http://www.davehunter.co.uk/blog">http://www.davehunter.co.uk/blog</a>Thu, 18 Jun 2009 20:55:19 Z2009-06-18T20:55:19Zhttp://social.technet.microsoft.com/Forums/en-US/sharepointdevelopment/thread/31e038a0-cf89-4e7c-bcee-9bd1ddaf9635#807eddb4-cf91-4f2c-8772-e8b3cdf5fe7bhttp://social.technet.microsoft.com/Forums/en-US/sharepointdevelopment/thread/31e038a0-cf89-4e7c-bcee-9bd1ddaf9635#807eddb4-cf91-4f2c-8772-e8b3cdf5fe7bCodeHawkhttp://social.technet.microsoft.com/Profile/en-US/?user=CodeHawkSetting custom masterpage doesn't work for new subsites<p>Hi,<br/>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. </p> <p>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.  <br/>2. Click Master Page under the Look and Feel group.<br/>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.</p> <p>If there are errors:<br/>1. Ensure the master page is part of the Master Page Gallery:<br/>Open the top level site in a browser. On the Site Actions menu, point to Site Settings, and then click Modify All Site Settings.<br/>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.</p> <p>2. Ensure the master page is published and approved:<br/>Open the top level site in a browser. On the Site Actions menu, point to Site Settings, and then click Modify All Site Settings.<br/>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.<br/>Using the drop-down menu associated with the master page file name, select Publish.<br/>After publishing, use the drop-down menu again to select Approve.</p> <p>3. Review  <a href="http://support.microsoft.com/default.aspx/kb/936908">http://support.microsoft.com/default.aspx/kb/936908</a><a href="http://support.microsoft.com/default.aspx/kb/936908"></a></p><hr class="sig">MCSD (VS 6)Fri, 03 Jul 2009 16:11:40 Z2009-07-03T16:11:40Zhttp://social.technet.microsoft.com/Forums/en-US/sharepointdevelopment/thread/31e038a0-cf89-4e7c-bcee-9bd1ddaf9635#1320f320-b466-4b5e-9a09-00401e71cde2http://social.technet.microsoft.com/Forums/en-US/sharepointdevelopment/thread/31e038a0-cf89-4e7c-bcee-9bd1ddaf9635#1320f320-b466-4b5e-9a09-00401e71cde2Dave Hunterhttp://social.technet.microsoft.com/Profile/en-US/?user=Dave%20HunterSetting custom masterpage doesn't work for new subsitesCodeHawk - This is a manual process bs_stuff wanted this to be automatically. Feature stapling is required if you want this to be set automatically.<hr class="sig">My SharePoint Blog - <a href="http://www.davehunter.co.uk/blog">http://www.davehunter.co.uk/blog</a>Fri, 03 Jul 2009 16:17:19 Z2009-07-03T16:17:19Zhttp://social.technet.microsoft.com/Forums/en-US/sharepointdevelopment/thread/31e038a0-cf89-4e7c-bcee-9bd1ddaf9635#67b7ed8c-de49-47f3-aa7a-a37be6dbf252http://social.technet.microsoft.com/Forums/en-US/sharepointdevelopment/thread/31e038a0-cf89-4e7c-bcee-9bd1ddaf9635#67b7ed8c-de49-47f3-aa7a-a37be6dbf252CodeHawkhttp://social.technet.microsoft.com/Profile/en-US/?user=CodeHawkSetting custom masterpage doesn't work for new subsites<p>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.<br/><br/>Just a few references:</p> <p>Beginning SharePoint 2007 By Amanda Murphy, Shane Perran</p> <p>SharePoint 2007 User's Guide By Seth Bates, Tony Smith</p> <p>Microsoft® Office SharePoint® Server 2007 By David Matthew Sterling</p> <p>PROFESSIONAL SHAREPOINT 2007 DESIGN By Jacob J. Sanford, Randy Drisgill, David Drinkwine, Coskun Cavusoglu, Heather Solomon</p> <p>Beginning SharePoint 2007 Administration By Göran Husman<br/></p><hr class="sig">Happy Coding, ~CodeHawk MCSD | MCTS [SharePoint Development]Fri, 03 Jul 2009 17:03:51 Z2009-07-03T17:03:51Zhttp://social.technet.microsoft.com/Forums/en-US/sharepointdevelopment/thread/31e038a0-cf89-4e7c-bcee-9bd1ddaf9635#fc9c9f25-32e4-4094-bc76-c8cf5251f48bhttp://social.technet.microsoft.com/Forums/en-US/sharepointdevelopment/thread/31e038a0-cf89-4e7c-bcee-9bd1ddaf9635#fc9c9f25-32e4-4094-bc76-c8cf5251f48bTony_is_herehttp://social.technet.microsoft.com/Profile/en-US/?user=Tony_is_hereSetting custom masterpage doesn't work for new subsitesHi CodeHawk,<br/>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.<br/>This is for custom masterpages.<br/><br/>Thu, 06 Aug 2009 09:04:43 Z2009-08-06T09:04:43Zhttp://social.technet.microsoft.com/Forums/en-US/sharepointdevelopment/thread/31e038a0-cf89-4e7c-bcee-9bd1ddaf9635#f0cb584f-3316-4241-a192-849fd25f3dd4http://social.technet.microsoft.com/Forums/en-US/sharepointdevelopment/thread/31e038a0-cf89-4e7c-bcee-9bd1ddaf9635#f0cb584f-3316-4241-a192-849fd25f3dd4CodeHawkhttp://social.technet.microsoft.com/Profile/en-US/?user=CodeHawkSetting custom masterpage doesn't work for new subsites<p>Hi Tony_is_here,<br/>Perhaps you didn't review the references I posted.<br/><br/>To clarify:  <br/>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.<br/><br/>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.<br/><br/>Too often fellow developers are quick to add additional complexity because they lack the knowledge of SharePoint 2007's inherent abilities. <br/><br/>Best Regards</p><hr class="sig">Happy Coding, ~CodeHawk MCSD | MCTS [SharePoint Development]Thu, 06 Aug 2009 12:25:22 Z2009-08-06T12:25:22Zhttp://social.technet.microsoft.com/Forums/en-US/sharepointdevelopment/thread/31e038a0-cf89-4e7c-bcee-9bd1ddaf9635#24c919ef-add9-4cb0-80a9-0450c53d8a4ehttp://social.technet.microsoft.com/Forums/en-US/sharepointdevelopment/thread/31e038a0-cf89-4e7c-bcee-9bd1ddaf9635#24c919ef-add9-4cb0-80a9-0450c53d8a4eTony_is_herehttp://social.technet.microsoft.com/Profile/en-US/?user=Tony_is_hereSetting custom masterpage doesn't work for new subsitesMost 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.<br/> <br/>Thu, 06 Aug 2009 13:04:01 Z2009-08-06T13:04:01Zhttp://social.technet.microsoft.com/Forums/en-US/sharepointdevelopment/thread/31e038a0-cf89-4e7c-bcee-9bd1ddaf9635#e6950531-7e3c-4afb-b2ff-3e7147b72a6bhttp://social.technet.microsoft.com/Forums/en-US/sharepointdevelopment/thread/31e038a0-cf89-4e7c-bcee-9bd1ddaf9635#e6950531-7e3c-4afb-b2ff-3e7147b72a6bTony_is_herehttp://social.technet.microsoft.com/Profile/en-US/?user=Tony_is_hereSetting custom masterpage doesn't work for new subsites<p>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.</p>Fri, 07 Aug 2009 08:04:43 Z2009-08-07T08:04:43Z