PowerShell Script to Limit SharePoint Site Templates Available

   I had a customer request recently to limit the which site templates were available to end users to create subsites.  Below is a short PowerShell script to do just that.

 

Problem

   If you are using a Publishing Site you can restrict the available site templates by going to Site Settings –> Look & Feel and clicking on Page Layouts and Site Templates (see following screenshot).  If you are not on a Publishing Site then there is not an easy (supported) way to accomplish this through out of the box means.

LimitSPSiteTemplates1

 

Solution

   The SPWeb class has methods GetAvailableWebTemplates and SetAvailableWebTemplates that can be used in conjunction with each other to get the current list of templates and then filter down to only the desired templates.  The filtered down results can then be set and updated on the SPWeb.  A version of this script can be downloaded from the TechNet Script Repository: Limit available web templates for a SharePoint site.

Note:  Be sure to modify the “<URL of site>” to your site’s URL and also change which templates to keep (line 2).

 

# array of template names (not titles) to keep 
$templateNamesToKeep = “STS#0”,“PROJECTSITE#0”,“BLOG#0” 

Start-SPAssignment -Global 
$web = Get-SPWeb <URL of site> 
# get the existing web templates from the site that will be filtered down 
# 1033 is the locale id for English US (en-us), be sure to change to your locale 
$existingWebTemplates = $web.GetAvailableWebTemplates(1033) 
$newWebTemplates = New-Object “System.Collections.ObjectModel.Collection[Microsoft.SharePoint.SPWebTemplate]” 
# filter existing web templates and only keep if in the list of template names to keep 
$newWebTemplates = $existingWebTemplates | Where-Object {$_.name -in $templateNamesToKeep} 
$web.SetAvailableWebTemplates($newWebTemplates, 1033) 
$web.Update() 
Stop-SPAssignment -Global 

   Before you run the script you will see the original list of available web templates when creating a subsite.

LimitSPSiteTemplates2

 

   After you run the script you will now see only the templates which you listed in the templates to keep.

LimitSPSiteTemplates3

 

   If you need to reset the list of available web templates you can execute the AllowAllWebTemplates method on the SPWeb object.

 

Conclusion

   I had run across a few other samples for limiting web templates that involved modifying out of the box SharePoint files (changes which may be overwritten by future updates or patches, and thus not recommended).  This process of using the SetAvailableWebTemplates() method appears to be a bit more safe and supported.

 

      -Frog Out

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s