Restricting Available Templates in MOSS07

Some companies have a list of corporate approved site templates that they only want users to be able to select when creating new sites.  One way that I found to restrict this is to create a Feature that uses the WSS OM to change the list of available template.  You could then “staple” this feature to templates that you want a restricted list of site templates on.

       public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPWeb site = (SPWeb)properties.Feature.Parent;

            site.AllowUnsafeUpdates = true;

            // Standard Publishing Feature must be activated
            PublishingWeb publishWeb = PublishingWeb.GetPublishingWeb(site);
            publishWeb.Web.AllowUnsafeUpdates = true;

            // Create a collection to store selected SPWebTemplates
            Collection<SPWebTemplate> temp = new Collection<SPWebTemplate>();

            //Get all available templates for a given lanugage
            Microsoft.SharePoint.SPWebTemplateCollection templates = site.GetAvailableWebTemplates(site.Language);

            // Add specific templates to our collection
            temp.Add(templates["STS#0"]);
            temp.Add(templates["MPS#1"]);
            temp.Add(templates["MPS#2"]);

            // Update the templates that are available
            publishWeb.SetAvailableCrossLanguageWebTemplates(temp, false);
            publishWeb.Update();

            site.AllowUnsafeUpdates = false;
        }

Leave a Reply