theme-sticky-logo-alt

Create a SharePoint Site Collection with a custom Template using Powershell

6 Comments

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.

Previous Post
Restrict the New Site Available Templates with Powershell
Next Post
Create Record Center Content Organizer rules using Powershell

6 Comments

  • June 20, 2013 at 11:30 am
    jamil haddadin

    Big thank you! this is the simplest way so far!

    Reply
  • March 17, 2014 at 8:07 am
    Robert

    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

    Reply
    • June 17, 2014 at 12:31 pm

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

      Reply
  • March 16, 2015 at 1:51 am

    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.

    Reply
  • November 9, 2018 at 10:20 am
    David Remillard

    Hi, I just found this article while looking for how to do this in SP Online. I didn’t see the GetWebTemplates method in either powershell SPO or PnP. Also, as soon as I create the site, even if I don’t specify a template, it auto applies STS#0 instead of leaving the rootweb empty. Is there a way to do this with SharePoint Online? Thanks, Dave

    Reply

Leave a Reply

15 49.0138 8.38624 1 0 4000 1 https://vladtalkstech.com 300 1