I recently had to work on a project where I had to deploy a Site Collection, with the root web using a Custom Template. It sounds easy to do with the GUI, “Select Template Later”, than you choose your template and that’s it. If you want to script it, it’s trickier (until you do it the first time ).
Here is how you create a new site using a custom .wsp template with PowerShell.
We Create the Site Collection. Note that we do not put the –template parameter as we do not want it to have a template.
$mainurl = “http://vlad.test.loc/sites/customtemplate” Write-Host -ForegroundColor White ” – Creating Site Collection $mainurl…” -NoNewline New-SPSite $mainurl -OwnerAlias locadministrator -Name “This will have a custom template” Write-Host -ForegroundColor GREEN “Done.”
Afterwards, we have to add our WSP to the Solution Gallery in the Site Collection. I have a Do While loop to make sure that the solution has been uploaded successfully before activating it.
Write-Host -ForegroundColor White ” – Adding and Installing Your Custom Template…” –NoNewline] Add-SPUserSolution -LiteralPath C:UsersAdministrator.locDesktoptestcustomtemplatevladcustomtemplate.wsp -Site $mainurl $ErrorActionPreference = “silentlycontinue” Write-Host “.” -NoNewline -ForeGroundColor White; $testsolution = Get-SPUserSolution -Identity vladcustomtemplate.wsp -Site $mainurl $ErrorActionPreference = “stop” Install-SPUserSolution -Identity vladcustomtemplate.wsp -Site $mainurl Write-Host -ForegroundColor GREEN “Done.”
Now that we got our empty site collection, we need to find the ID of the template we want to apply to it. Here is the Command.
$site= new-Object Microsoft.SharePoint.SPSite($mainurl ) $loc= [System.Int32]::Parse(1033) $templates= $site.GetWebTemplates($loc) foreach ($child in $templates) write-host $child.Name ” ” $child.Title
At the end of the list, you will find your Custom Template GUID and Name. Look at the picture below.
Note that the Template GUID and Name will never change. So if you want to send the script and the package to someone else, you can hardcode this part and it will work no problem. The last part, is applying the template to our new Site.
$web = GET-SPWeb $mainurl
After, you can access your site and your custom template will be applied. You can see in the screenshot below that there is a customer Document Library (“Custom Document Library”) and that it inherited the template description from my Template.