Create SharePoint Site (SPWeb) using Custom Template and PowerShell

I got a task to setup company’s hierarchy using our own custom created site template. My first priority is to achieve this task through PowerShell. Creating site collection or sub site through PowerShell is fairly a simple task, the command for creating sub site is

New-SPWeb http://sharepoint/subweb1 -Template “STS#0”

Ref: http://technet.microsoft.com/en-us/library/ff607579(v=office.15).aspx

I tried to replace the template name with my custom template’s name which I found out through the below script:

Add-PSSnapin Microsoft.Sharepoint.Powershell
$web = get-spweb https://sharepoint
$template = $web.GetAvailableWebTemplates(1033) | Where-Object {$_.Title -eq “<Custom Templete Title>”}
Write-Host $template.Name

Create SharePoint Site (SPWeb) using Custom Template and PowerShell

In the below script substitute the default template name with my custom template name but this didn’t work

New-SPWeb -Url “https://sharepoint/newsubsite” -Template “{D839E38A-F9DF-4B35-BDA7-A1E273C51EAB}#XXXX Template”

 Output:

Create SharePoint Site (SPWeb) using Custom Template and PowerShell

After looking into documentation the parameter it takes as Template is SPWebTemplatePipeBind which is optional. If someone doesn’t set the template, the site will be created but the template isn’t applied which will then prompt for template selection against its first visit. Finally I came up with the below script which worked like a charm.

 Add-PSSnapin Microsoft.Sharepoint.Powershell
$web = get-spweb https://sharepoint
$template = $web.GetAvailableWebTemplates(1033) | Where-Object {$_.Title -eq “<Custom Templete Title>”}
$newweb = New-SPWeb -Url “https://sharepoint/newsubsite” 
$newweb.ApplyWebTemplate($template.Name)
The following two tabs change content below.
Muhammad Sami Ahmed

Muhammad Sami Ahmed

With over 9 years of extensive working experience on different projects and products in SharePoint server, ASP.NET, C#, WCF, WPF, LINQ, SQL Server. He is working on SharePoint for past five years. He is skilled and proficient in systems architectural design and development of complex SharePoint web applications. The development background gives him an edge in designing the end solution to exploit the full capabilities of SharePoint.

One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *