0

I'm creating a site collection and trying to apply a template to it

Here is the code:

$Siteurl = "http://myserver:8010/sites/sitex5"

New-SPSite $Siteurl  -OwnerAlias "mydomain\sp2k13admin" -Name "This will have a custom template" -Language 1040

Add-SPUserSolution -LiteralPath c:\temp\modelloit.wsp -Site $Siteurl


do

{
Write-Host "." -NoNewline -ForeGroundColor White;

    Start-Sleep -Seconds 5;

        try {
            $testsolution = Get-SPUserSolution -Identity modelloit.wsp -Site $Siteurl
            }
        catch{}

    } while(!$testsolution);

# it never gets to here as I see on the screen the dots to check for progression

Install-SPUserSolution -Identity modelloit.wsp -Site $Siteurl

Write-Host "Site collection with custom template is created"

The site gets created but when I open it, it seems that the template was not set as I get redirected to:

http://myserver:8010//sites/sitex5/_layouts/15/templatepick.aspx

enter image description here

So what am I missing to set a template properly after the creation of a site collection?

EDIT I have tried setting the template afterwards

$web = Get-SpWeb $Siteurl

$template = $web.GetAvailableWebTemplates(1033) | Where-Object {$_.Title -eq "modelloit"}

Write-host $template.Name #I see that it is correctly printend

Set-SPSite -Identity $Siteurl -Template $template.Name 

but I get :

WARNING: Template is not found and is not applied.

EDIT2 it works if instead of Set-SPSite I use

$web.ApplyWebTemplate($template.Name)
  • 1
    I think you are creating the site collection, then install the solution( i believe a template), But did you try to set the template via PowerShell after the install feature.I am not sure what's in code which is restraining it from applying. – Waqas Sarwar MVP Jun 9 '16 at 15:31
  • I've tried setting afterwards as you can see in edit but no luck – Gyonder Jun 10 '16 at 8:29
  • #Waqas Sarwar MCSE see edit and edit2. If you want to put it as solution I'll flag it. – Gyonder Jun 10 '16 at 8:39
0

You need to write PowerShell in 2 parts. One will create site collection and the other will apply custom template.

Start Part 1

Create a SharePoint Site Collection with a custom Template using Powershell

Write-Host “Provide the site collection URL e.g. http://intranet/site/custom”

$Siteurl = read-host

Write-Host “Provide the site collection owner email address”

$owner = read-host

New-SPSite $Siteurl -OwnerAlias $owner -Name “This will have a custom template”

End Part 1 :- Create a SharePoint Site Collection with a custom Template using Powershell

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

Start Part 2

Create a SharePoint Site Collection with a custom Template using PowerShell

Add-SPUserSolution -LiteralPath d:\temp.wsp -Site $Siteurl

$ErrorActionPreference = “silentlycontinue”

do

{

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

Start-Sleep -Seconds 5;

try

{

$testsolution = Get-SPUserSolution -Identity temp.wsp -Site $Siteurl

}

catch

{}

} while(!$testsolution);

$ErrorActionPreference = “stop”

Install-SPUserSolution -Identity temp.wsp -Site $Siteurl

Write-Host “Site collection with custom template is created”
  • Have you tried above solution? – Hardik Jun 9 '16 at 18:54
  • But this is just what I wrote in my post. – Gyonder Jun 10 '16 at 6:54
  • You need to follow the step which I mentioned in my answer in serialized form... – Hardik Jun 10 '16 at 6:58
  • Do you mean that I have to make two scripts and launch them separately? – Gyonder Jun 10 '16 at 7:14
  • Yes of course... – Hardik Jun 10 '16 at 7:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.