Create a SharePoint Site Collection with a custom Template using Powershell

Create a SharePoint Site Collection with a custom Template using Powershell

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

         Write-Host

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”

                do

                {

Write-Host “.” -NoNewline -ForeGroundColor White;

                                Start-Sleep -Seconds 5;                                

                                try

                                {

                                $testsolution = Get-SPUserSolution -Identity vladcustomtemplate.wsp -Site $mainurl

                                }

                                catch

                                {}

                } while(!$testsolution);

          $ErrorActionPreference = “stop”

          Install-SPUserSolution -Identity vladcustomtemplate.wsp -Site $mainurl

          Write-Host -ForegroundColor GREEN “Done.”

           Write-Host  

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

}

$site.Dispose()

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

$web.ApplyWebTemplate(“{B8042B86-989C-4D59-A57C-D06918681DE1}#vladcustomtemplate”)

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.

5/5 (1)

Please rate this

 
Comments

Salut Vlad,

I was using your code to create new Site Collections using a custom template. Somehow I get this error and the template will not apply:
Exception calling “ApplyWebTemplate” with “1” argument(s): “File or arguments not valid for site template ‘{F3F85CEF-E2E4-4B01-BC10-7294ED243FB2}#NewTemplate’. Parameter name: WebTemplate”…
do you have any idea why this should fail. When I try, on the same SC, to create a subsite using the already activated custom template, via GUI, this will work just fine.

any help will be appreciate,
Regards,
Robert

Did you do it step by step and you have the good GUID, as well as upload the solution and activate it?

Just found this post and it helped me heaps. Thanks a lot!

Instead of hard coding the GUID, you can do this instead:

$loc = [System.Int32]::Parse(1033)
$opsTemplate = $baseSite.GetWebTemplates($loc) | ?{ $_.Title -eq “ExtranetOperations” }
$opsTemplateId = $opsTemplate.Name.Split(“#”)[0]
$operationsWeb.ApplyWebTemplate(“$opsTemplateId”)

I have included this in an automated system and thought that this might help others doing the same as me.

Thanks for sharing Chris!

Leave a Reply