Friday, June 15, 2012

Feature that can be used to control the list of available web templates

If you would like to control the list of possible web templates that are available on a SharePoint site to users who are authorized to add sites, I'd like to present some code that you can add to a feature that will allow you to adjust the list of possible options.  In case you aren't certain what I mean by these available web templates, you can view this list by navigating to your SharePoint site and clicking on Site Actions -> New Site and you will be presented with a list of possible templates to choose from.

In the code sample below, activating the feature will remove all possible web templates with the exception of the standard Document Workspace template from the display list.  Deactivating the feature will allow the list of options to return to its normal mode of displaying all web templates:

public override void FeatureActivated(SPFeatureReceiverProperties properties) { try { SPWeb web = properties.Feature.Parent as SPWeb; // Hide all available web templates with the exception of the Document Center web template SPWebTemplateCollection currentTemplates = web.GetAvailableWebTemplates(1033); Collection newTemplates = new Collection(); foreach (SPWebTemplate currentTemplate in currentTemplates) { if (currentTemplate.Title.Trim() == "Document Workspace") { newTemplates.Add(currentTemplate); } } web.SetAvailableWebTemplates(newTemplates, 1033); web.Update(); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.ToString()); } } public override void FeatureDeactivating(SPFeatureReceiverProperties properties) { try { SPWeb web = properties.Feature.Parent as SPWeb; // Show all available web templates web.AllowAllWebTemplates(); web.Update(); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.ToString()); } }

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.