Show Web Template

Carrying on from my previous post Hide Web Template, this post demonstrates how to show a web template in the New Site dialog for a specific web using a PowerShell script:

function Add-SPWebTemplate {
	[CmdletBinding()]
	param(
		[parameter(Position=1,Mandatory=$true)][Microsoft.SharePoint.SPWeb]$web
	)
	process {
		$templates = New-Object "System.Collections.ObjectModel.Collection``1[[Microsoft.SharePoint.SPWebTemplate, Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c]]"
		$availableWebTemplates = $web.GetAvailableWebTemplates(1033);
 
		$availableWebTemplates | ForEach-Object {
			if ($_.ID -eq 53) {
				Write-Host ("Blank Internet web template is already enabled for web '", $_.Title, "' at ", $_.Url -join "") -NoNewline
				return
			}
 
			$templates.add($_)
		}
 
		$web.AllowAllWebTemplates()
		$allWebTemplates = $web.GetAvailableWebTemplates(1033);
 
		$allWebTemplates | Where{ $_.ID -eq 53 } | ForEach-Object {
			$templates.add($_)
		}
 
		Write-Host ("Enabling Blank Internet web template for web '", $_.Title, "' at ", $_.Url -join "") -NoNewline
		$web.SetAvailableWebTemplates($templates, 1033);
		$web.Update()
		Write-Host " - Done." -ForegroundColor Green
	}
}
 
 
Get-SPWebApplication | ForEach-Object {
	$_.Sites | Where { $_.ServerRelativeUrl -ne '/sites/example' } | ForEach-Object {
		$_.AllWebs | ForEach-Object {
			Add-SPWebTemplate $_
		}
	}
}

The method Add-SPWebTemplate is called for each web where we want to show a particular template in the New Site dialog.

First, we get a list of all currently enabled site templates by calling the GetAvailableWebTemplates method of the passed SPWeb object. We then check to see if the template we are looking to enable is already configured to be shown in the new Site dialog and where it is the script stops as there’s nothing else to do.

Assuming the template was not found and therefore is not enabled, we maintain a collection of all currently enabled templates in the variable $templates

Next, we call the method AllowAllWebTemplates and then call the GetAvailableWebTemplates method once more. All templates will be returned, including those that are not enabled.

This time, when we find the template matching the one we want, Blank Internet in this example, we add it to the $templates variable.

Finally, we call the SetAvailableWebTemplates, pass it the $templates variable, which includes the web template we want to enable, and call Update on the web to save the changes.

The webs have been restricted to those in the site collection ‘/sites/example’ for this example.

This entry was posted in SharePoint and tagged , . Bookmark the permalink.
0 0 votes
Article Rating
Subscribe
Notify of
guest

Solve the maths problem shown below before posting: *

0 Comments
Inline Feedbacks
View all comments