Create a SharePoint Subsite from Custom Site Template using PowerShell
Requirement: We have a PMO site template created for managing projects in SharePoint. Had a new requirement to create some 20+ new PMO subsites based on the custom site template.
To create a site based on site template, you can pick the custom tab and then select appropriate site template from the list.
PowerShell script to create a subsite from custom site template:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#Custom function to create a site from site template
Function Create-SubsiteFromTemplate($SiteTitle, $ParentSiteUrl, $TemplateTitle)
{
#Get the custom site template
$ParentWeb = Get-SPWeb $ParentSiteUrl
$SiteTemplates = $ParentWeb.GetAvailableWebTemplates($ParentWeb.Language)
$CustomSiteTemplate = $SiteTemplates | Where-Object {$_.Title -eq $CustomSiteTemplateTitle}
#URL for you new subsite
$SiteUrl = $ParentSiteUrl + $SubsiteURL
#create subsite without template
$web = New-SPWeb -Name $SiteTitle -Url $SiteUrl
#Apply the custom site template
$Web.ApplyWebTemplate($CustomSiteTemplate.Name)
Write-host "Subsite $($web.Url) has been created from template!"
}
#Call the function to create subsite
Create-SubsiteFromTemplate "Airbus 365 Project" "https://teamsites.crescent.com/projects/" "PMO Site Template"
This creates s subsite “Airbus 365 Project” with URL “airbus365project” at the parent site: https://teamsites.crescent.com/projects/ with template “PMO Site Template”.
How to create a subsite in bulk from a custom site template?
#List of sites to be created
$ProjectList = @("Data Tools", "Knowledge Village", "Service Center", "Business Reporting", "JDT Core", "EFI", "Ecore Tools")
$ParentSiteURL ="https://teamsites.crescent.com/projects/"
$SiteTemplateTitle = "PMO Site Template"
#loop through each object in the array and create subsite
foreach($Project in $ProjectList)
{
#Call the function to create subsite
Create-SubsiteFromTemplate $project $ParentSiteURL $SiteTemplateTitle
}
I do trust all of the ideas you have presented in your post.
They are really convincing and can certainly
work. Nonetheless, the posts are very short for beginners.
May just you please extend them a bit from next time? Thank you for the post.
am getting the error:
New-SPWeb : The Web site address “/sites/dms” is already in use.
At C:createsubsitefromsitetemplate.ps1:20 char:22
+ $web = New-SPWeb <<<< -Name $SiteTitle -Url $SiteUrl + CategoryInfo : InvalidData: (Microsoft.Share....SPCmdletNewWeb:SPCmdletNewWeb) [New-SPWeb], SPException + FullyQualifiedErrorId : Microsoft.SharePoint.PowerShell.SPCmdletNewWeb You cannot call a method on a null-valued expression. At C:createsubsitefromsitetemplate.ps1:23 char:26 + $Web.ApplyWebTemplate <<<< ($CustomSiteTemplate.Name) + CategoryInfo : InvalidOperation: (ApplyWebTemplate:String) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull
Apparently, the error message says the New-SPWeb already exists! So if you want to apply custom web template on existing sites, use:
$web = Get-SPWeb
$web.ApplyWebTemplate($CustomSiteTemplate.Name)