web provisioning feature that forces new team sites to inherit parent's page layout and site templates settings- how?

已答覆 web provisioning feature that forces new team sites to inherit parent's page layout and site templates settings- how?

  • Thursday, April 28, 2011 8:38 AM
     
     
    I'm not well versed with C# and need some help writing a web provisioning feature that will force new sites created to inherit their parents page layout and site templates settings.  thanks for the help
    -Chad

All Replies

  • Thursday, April 28, 2011 10:54 AM
     
     

    Have a look at this thread, the two code samples there have all the info you need.

    http://social.msdn.microsoft.com/Forums/en/sharepoint2010general/thread/e2d0dcaa-ae7d-4609-bf4e-5dfddd86f247


    http://jcapka.blogspot.com
    My company XComplica is hiring!
  • Thursday, April 28, 2011 4:47 PM
     
     

    I already have a web provision feature that sets the masterpage for any new sites created...that's not what I'm looking for.  I'd like to have the feature not only continue to apply the root masterpage, but also inherit the parent's page layout and site template settings.

    If you go to Site Actions/Site Settings/Page Layout and Site Templates....you'll see the list of templates...and we are only making available certain templates.  However since we have created custom site templates for team sites...when provisioning these team sites they do not inherit the same limited list of templates the parent has.  I could manually check the box and force subsites to inherit the templates...however I'm not going to do that on a daily basis just to update the custom team sites that get created.  Rather a webprovision feature should be used to tell any new sites to inherit those settings....just like it does for the masterpage.

    hopefully that cleared it up. thanks for the help!


    -Chad
  • Thursday, April 28, 2011 5:19 PM
     
     

    I see. I would guess that you have to set one of the properties in the property bag to true, you'll have to experiment a bit with this.  I would use the little demo application on this page to see what changes when you check that checkbox you mention.

    http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spweb.properties.aspx


    http://jcapka.blogspot.com
    My company XComplica is hiring!
  • Friday, April 29, 2011 5:56 AM
     
     

    Hi Chad,

    I don't think it's possible to forcibly change the web template of a web. It means that we have no way to change the web template of a created web.

    For an example, if we created a team site based on team site template and you wanted to change the web to Blog site by changing the template of the site, how can we do that?

    For page layout  it works for publishing page. When creating a publishing page, you can set the layout page.

    For this topic you can see the following document.

    http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.publishing.publishingpage.layout.aspx

    http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.publishing.publishingweb.setdefaultpagelayout.aspx

    http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.publishing.publishingweb.isinheritingavailablepagelayouts.aspx

     


    Microsoft Online Community Support
  • Friday, April 29, 2011 2:54 PM
     
      Has Code

    I'm not trying to change the template of a current web.  I just simply want any new subsites during creation that are created via a custom template to inherit the subsite templates the root parent is has made available.  Let me spell out the scenario so no one is confused here:

    1. Go to root site collection http://intranet, click on site actions, site settings, "Page Layout and Site Templates".
    2. Select "Subsites can only use the following site templates:" and choose blank and blog, then check "Reset all subsites to inherit these preferred subsite template settings" and hit OK.
    3. Go create a new blank subsite (since it's that or a blog as the only available templates to use), customize to your hearts desire, then save it as a template...we'll call it "Custom Team Site"
    4. Go back to the root site collection "Page Layout and Site Templates" settings page and add in our new "Custom Team Site" to this list of available templates, then check "Reset all subsites to inherit these preferred subsite template settings" so that all our subsites will now have this new "Custom Team Site" available to them to use for creating new subsites.
    5. Now go create a new subsite based off this "Custom Team Site" template, we'll call it Accounting:  http://intranet/accounting ...everything is looking good so far.
    6. Now Accounting needs a subsite called "Payroll" and we want to use the "Custom Team Site" template to create the "Payroll" site....but wait!...the "Custom Team Site" template isn't available in the list of templates to create anymore within the Accounting site.  That's because when the "Custom Team Site" template was created it makes a ghosted version of the site and takes with it the list of available template...which at the time were only "blank" and "blog".

    The manual solution to this is to go back to the site collection root and check "Reset all subsites to inherit these preferred subsite template settings" so that the "Accounting" subsite will now have the "Custom Team Site" template in it's arsenal for creating subsites like "Payroll".  Or you can also just append this url "_Layouts/AreaTemplateSettings.aspx" to http://intranet/accounting to take you to the page where you'll notice it's set to inherit the parent, but there you'll see it's only showing "Blank" and "Blog" and it's all greyed out....yet once you hit OK....it will correctly grab the new list of templates from the parent. 

    Not much of a solution for a site collection that has many new subsites created daily.  Not really wanting to schedule myself a reminder to "Reset all subsites to inherit these preferred subsite template settings" on a daily basis.

    So what I'm looking for is a web provisioning event receiver to tell the new site to inherit the parent's subsite template list.

    Here's the current event receiver i have for provisioning team sites to grab the masterpage...so surely there should be a way to add to this to have the new site inherit the parent subsite template list:

    using System;
    using Microsoft.SharePoint;
    
    namespace Intranet.Branding.Branding
    {
      public class WebEventReceiver
        : SPWebEventReceiver
      {
        public override void WebProvisioned(SPWebEventProperties properties)
        {
          SPWeb site = properties.Web;
          SPWeb rootSite = site.Site.RootWeb;
          site.MasterUrl = rootSite.MasterUrl;
          site.CustomMasterUrl = rootSite.CustomMasterUrl;
          site.Update();
        }
      }
    }

    Please don't suggest I enable Publishing on the web via stapling.  Remember it is not supported to create custom site templates from sites with Publishing features enabled.  Also if you do staple publishing features to a new site created from a custom site template....you still have to after the fact go to "Page Layout and Site Templates" settings on that new site and hit OK once so it will update it's subsite template list from the parent = yet another manual solution.

    thanks for the help.  Again I'm not a developer and so digging through the different sharepoint methods, class, properties and how they work is foreign to me.  The masterpage event receiver however was well published on the intranet so I was able to find it.


    -Chad
  • Sunday, May 01, 2011 7:06 PM
     
     

    There is a property in the SPWeb properties collection called __WebTemplates that has the allowed templates specified in it. The content is just XML, so try setting that to have your web template in it. If you need the necessary xml, use powershell to get the xml inside of your root site which already has your custom template included.

    Powershell:

    $root = Get-SPWeb <rootUrl>

    $root.AllProperties["__WebTemplates"]

    This will spit out the xml you need

    C# (add to your code above)

    site.AllProperties["__WebTemplates"] = XmlFromPowershell;

    You can also try something like this to be more dynamic:

    site.AllProperties["__WebTemplates"] = site.Site.RootWeb.AllProperties["__WebTemplates"];

     

     


    http://jcapka.blogspot.com
    My company XComplica is hiring!
    • Edited by Joe Capka Sunday, May 01, 2011 7:08 PM error in code example
    •  
  • Tuesday, May 03, 2011 4:13 PM
     
     Answered Has Code

    One of our internal developers wrote a solution last saturday that is working great:  I left in the commented out section for those that want to grab the masterpage from the site collection root....We however wanted new sites to grab a masterpage different than the root site collection...so thus you'll see a custom.master one mentioned.  However beyond the masterpage stuff you'll see how the new provisioned site is grabbing the list of approved templates from the site root and applying them to the new site.  It works great and doesn't have the performance burden of forcing the root site to push susbsite template inheritance all the time. 

    using System;
    using Microsoft.SharePoint;
    
    namespace Intranet.Branding.Branding
    {
      public class WebEventReceiver : SPWebEventReceiver
      {
        public override void WebProvisioned(SPWebEventProperties properties)
        {
          using (SPWeb currentWeb = properties.Web)
          {
            currentWeb.AllowUnsafeUpdates = true;
            using (SPWeb rootWeb = currentWeb.Site.RootWeb)
            {
              rootWeb.AllowUnsafeUpdates = true;
              string WebAppRelativePath = rootWeb.ServerRelativeUrl + (rootWeb.ServerRelativeUrl.EndsWith("/") ? System.String.Empty : "/");
    
              //currentWeb.MasterUrl = rootWeb.MasterUrl;
              currentWeb.MasterUrl = WebAppRelativePath + "_catalogs/masterpage/custom.master";
              //currentWeb.CustomMasterUrl = rootWeb.CustomMasterUrl;
              currentWeb.CustomMasterUrl = WebAppRelativePath + "_catalogs/masterpage/custom.master";
              currentWeb.Update();
              System.Collections.ObjectModel.Collection<SPWebTemplate> currentTemplates = null;
              foreach (System.Object language in rootWeb.RegionalSettings.InstalledLanguages)
              {
                if (language is SPLanguage)
                {
                  currentTemplates = new System.Collections.ObjectModel.Collection<SPWebTemplate>();
                  foreach (System.Object currentTemplate in rootWeb.GetAvailableWebTemplates((System.UInt32)((SPLanguage)language).LCID))
                  {
                    if (currentTemplate is SPWebTemplate)
                    {
                      currentTemplates.Add((SPWebTemplate)currentTemplate);
                    }
                  }
                  if (currentTemplates.Count > 0)
                  {
                    currentWeb.SetAvailableWebTemplates(currentTemplates, (System.UInt32)((SPLanguage)language).LCID);
                  }
                }
              }
              currentWeb.Update();
            }
          }
        }
      }
    }
    
    

    -Chad
    • Marked As Answer by philldogger Tuesday, May 03, 2011 4:13 PM
    • Edited by philldogger Tuesday, May 03, 2011 4:21 PM code edit
    •