0
Add-PSSnapin Microsoft.SharePoint.PowerShell
$web = Get-SPWeb "http://SPWeb/sites/MyApps/Orders"
$template = $web.GetAvailableWebTemplates(1033) | Where-Object {$_.Title -eq "Order_Template"}
$newweb = New-SPWeb -Url "http://SPWeb/sites/MyApps/Orders/x2" -Name "X2"
$newweb.ApplyWebTemplate($template.name)

Sometimes ApplyWebTemplate gives error saying template is null. Sometimes it works.

If I hardcode the id it always work:

$newweb.ApplyWebTemplate("{2259B9C7-884F-41F5-8A77-95880CEE31FB}#Order_Template")

I know it crazy but it is happening.

0

Try to prevent automatic garbage collection using Start-SPAssignment as follows:

Add-PSSnapin Microsoft.SharePoint.PowerShell
Start-SPAssignment -Global
$web = Get-SPWeb "http://SPWeb/sites/MyApps/Orders"
$template = $web.GetAvailableWebTemplates(1033) | Where-Object {$_.Title -eq "Order_Template"}
$newweb = New-SPWeb -Url "http://SPWeb/sites/MyApps/Orders/x2" -Name "X2"
$newweb.ApplyWebTemplate($template.name)
Stop-SPAssignment -Global

For details, see Start-SPAssignment on Microsoft docs.

Also, see this related question: Object returned by Get-SPSiteAdministration cmdlet has no valid values

What you can also try is using the "-Template" argument of New-SPWeb:

$newweb = New-SPWeb -Url "http://SPWeb/sites/MyApps/Orders/x2" -Name "X2" -Template $template.Name

You will not have to use SPWeb.ApplyWebTemplate()

  • I did the changes you suggested but still the same result. – Shashi B Jun 22 '18 at 10:58
  • @ShashiB Ok... try the -Template argument instead or in combination with the Start-SPAssignment approach. – helb Jun 22 '18 at 11:17

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.