3

I have the following code to create a site and apply a custom template. It is working in my test environment that is created by backup-restore from my production portal.

This is the current script, based on the feedback in the comments:

#creates a site under the given url, based on the given template and title
Function CreateSite($parentUrl, $SubSiteUrl, $Template, $Title) {
    try {
        write-host "Read parent site"    
        $web = GET-SPWeb $parentUrl
        write-host "reading template"
        $wtemplate = $web.GetAvailableWebTemplates(1033) | Where-Object {$_.Title -eq $Template}   

        $url = $parentUrl + "/" + $SubSiteUrl
        write-host "creating site"
        $NewWeb = New-SPWeb -Url $url -Name $Title -UseParentTopNav
        $web = GET-SPWeb $url
        write-host "applying template"
        #System.Object[]
        $NewWeb.ApplyWebTemplate($wtemplate.Name)
        write-host "done"
        return "OK"

    }
                catch {

                $ErrorMessage = $_.Exception.Message
                if ($ErrorMessage -match 'is already in use.') {
               try{
             write-host "site exists, applying template"
               $web = GET-SPWeb $url
               $web.ApplyWebTemplate($wtemplate.Name)
             return "OK"
               }
               catch{
               $ErrorMessage = $_.Exception.Message
                 write-host $ErrorMessage
                return ($ErrorMessage)
               } 

                }

                write-host $ErrorMessage
                return ($ErrorMessage)
    }

    }

However, when I run it in my PRD server, I get the following error message on some of my sites:

Exception calling "ApplyWebTemplate" with "1" argument(s): "File or arguments not valid for site template 'System.Object[]'.

Update: Looks like we have double templates with the same title, but different names. However, these extra templates are not visible under "solutions" or in Designer, onder site templates like:

Name                                                Title
{A248FF57-5248-4C8B-9459-1B62703DACCD}#Customers    Customers
{CC69A307-7F92-47A7-8B6F-3981B480F9EA}#Customers    Customers

So, the task is to remove one of these templates, but I did not find any code that removes a web template by name. The Uninstall-SPSolution accepts the wsp file name as parameter, but there is only one customer.wsp in the solutions library, so this will probably remove both. Deactivating, removing and uploading template to the solution library didn't help.

1 Answer 1

3

Try to add .Name at this line

$Web.ApplyWebTemplate($wtemplate.Name)

Web.ApplyWebTemplate method has one argument that is the web template Name


[Update]

I modified your script and tried it on my side, it should be now working properly!

$parentUrl = "http://epm:19812"
$SubSiteUrl = "Qassas2019"
$tempname = "Test"
$web = GET-SPWeb $parentUrl
$wtemplate = $web.GetAvailableWebTemplates(1033) | Where-Object {$_.Title -eq $tempname}   
$url = $parentUrl + "/" + $SubSiteUrl
$NewWeb = New-SPWeb -Url $url -Name $Title -UseParentTopNav  
$NewWeb.ApplyWebTemplate($wtemplate.Name)

Make sure that

  • You have set all the variables like $tempname accurately,
  • The $tempname $wtemplate and $NewWeb are not null, by calling it as shown below.
  • No need to use set $web again with the new URL site, you should use the$NewWeb` directly.

enter image description here

[Update]

The below script will help you to remove an Orphaned Custom Site Template

$templateTitle = "TitleOfTemplateToBeZapped"

$site = Get-SPSite http://domain.com/sites/SiteColRoot

$templates = $site.GetWebTemplates(1033) #lcid, 1033 = U.S. 

$badTemplate = $templates | where { $_.Title -eq $templateTitle }

if ($badTemplate -ne $null) {
    # the hidden feature Id is the first part of the template 
    #    name for custom web templates
    $id = $badTemplate.Name.substring(0, $badTemplate.Name.IndexOf("#"))
    $badId = [System.Guid]($id)

    $site.Features.Remove($badId)
    write-output "Template '$templateTitle' has been removed"
} else {
    write-output "No template was found with the title of '$templateTitle'"
}

$site.Dispose()

Script Ref: Use PowerShell to Remove an Orphaned Custom Site Template from SharePoint

8
  • I tried, now I get another message: Exception calling "ApplyWebTemplate" with "1" argument(s): "Value cannot be null. Does this mean that the template is not found with the given name? Can I check with gui or Designer the Name of the templates?
    – vilmarci
    Aug 31, 2017 at 7:31
  • @vilmarci yes that means $wtemplate is null, one thing I have noted is using $Web instead of the $NewWeb at $Web.ApplyWebTemplate($wtemplate.Name), let me update you with a script that I tried on my side and worked properly Aug 31, 2017 at 10:22
  • @vilmarci do you checked the modified script? please feedback me in case you still face issue! Sep 8, 2017 at 10:41
  • 1
    Sorry for the late feedback, took some time to move test scripts to prd. So, what I found out is, that I get 2 different error messages: If I template title is correct, the updated script returns Exception calling "ApplyWebTemplate" with "1" argument(s): "Value cannot be null. If the template title is valid, but something unknown goes wrong, I get the Exception calling "ApplyWebTemplate" with "1" argument(s): "File or arguments not valid for site template 'System.Object[]'.' error message.
    – vilmarci
    Sep 12, 2017 at 11:56
  • NP, let me check your updates in my side and feedback! Sep 12, 2017 at 12:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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