Tuesday, March 11, 2014

Create a SharePoint Subsite from Custom Site Template using PowerShell

Requirement: We've 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.

PowerShell script to create 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

    Write-host "Subsite $($web.Url) has been created from template!"

#Call the function to create subsite
Create-SubsiteFromTemplate "Airbus 365 Project" "http://teamsites.crescent.com/projects/"  "PMO Site Template"
This creates s subsite "Airbus 365 Project" with URL "airbus365project" at the parent site: http://teamsites.crescent.com/projects/ with template "PMO Site Template".

How to create subsite in bulk from custom site template?
#List of sites to be created
$ProjectList = @("Data Tools", "Knowledge Village", "Service Center", "Business Reporting", "JDT Core", "EFI", "Ecore Tools")
$ParentSiteURL ="http://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    

You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.

Check out these SharePoint products:


  1. 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

    1. 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


Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...