Use PowerShell to Remove an Orphaned Custom Site Template from SharePoint 2010


A situation cropped up today where a custom Site Template was created by saving a site as a template which was then used and subsequently deactivated and then deleted. Simple enough except that SharePoint insisted that the template was still there and presented it as a choice when creating new sites even though it didn’t exist and would fail if someone clicked on it.

I could have simply gone into Site Settings – Page Layouts and Site Templates and hidden it there but that raises other problems as well as adds ongoing maintenance as it means all new templates would have to be explicitly added in order to be visible. However, even if I did that, it still doesn’t solve the underlying issue of having a corrupt Site Template in the system. A search showed lots of people with the exact same issue and all without any resolutions.

The key thing that unlocked this was realizing that when SharePoint creates a web template, it creates both the template (as a solution) and a hidden feature that represents it. In our case, the solution was gone (even from the Recycle Bin) but the feature lived on in sort of a half zombie state, being both alive and dead at the same time.

I came up with the script below to actually get rid of the web template feature and hope it helps others!

$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()
About these ads

Posted on May 6, 2013, in SharePoint and tagged , . Bookmark the permalink. 2 Comments.

  1. Awesome Post …. nice script!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 136 other followers