Home > Sharepoint > AvailableWebTemplates – for WSS

AvailableWebTemplates – for WSS

December 17th, 2008

MOSS publishing infrastructure gives the possibility to declare which web templates that are available on subwebs, in a feature property:

<Properties>
   <Property Key="AvailableWebTemplates" Value="*-MyTemplateName#1"/>
</Properties>

But this is not possible in the same way for WSS. The object model gives us two hints: SPWeb.SetAvailableWebTemplates and SPWeb.SetAvailableCrossLanguageWebTemplates These methods can be used to achieve the same goal: controlling which web templates that are available for creation from a certain site template in the UI (although I have not got the cross language version to work). A feature receiver in a feature stapled to my site template can be used to set which web templates that are available. The code for to set which templates are available are contained in a separate method:

/// <summary>
/// Set available templates for creating subwebs
/// </summary>
/// <param name="web">the web it concerns</param>
/// <param name="siteTemplateNamePredicate">how to match the template name</param>
public static void SetAvailableSubWebTemplates(
	SPWeb web, Predicate<String> siteTemplateNamePredicate,
	bool currentLocale)
{
    Collection<SPWebTemplate> collection = new Collection<SPWebTemplate>();
    foreach (SPWebTemplate template in web.GetAvailableWebTemplates(web.RegionalSettings.LocaleId))
   {
       if (template != null &&
           template.IsSubWebOnly &&
           siteTemplateNamePredicate.Invoke(template.Name))
       {
            collection.Add(template);
       }
   }
   if (collection.Count > 0)
   {
       web.SetAvailableWebTemplates(collection, web.RegionalSettings.LocaleId);
       web.Update();
   }
}

Finally, how to use this method from the feature receiver:

Predicate<String> predicate = new Predicate<string>(s => s.StartsWith("STS"));
SetAvailableSubWebTemplates(web, predicate);

peter Sharepoint , ,

  1. No comments yet.
  1. No trackbacks yet.