Is there way to use Set-ItemTemplate to fix an item with a broken (deleted) template?

I'm trying the following script but get a null reference error

$item = Get-Item -Path master: -Query "/sitecore/content/MyProject/Commerce Websites/MyWebsite/Data/Sliders/Option Folder"
$sliderDataFolderTemplate = Get-Item -Path master: -ID "{551BBAE9-84F4-4393-BB96-5BB410203B86}"

Set-ItemTemplate -ID $item.ID -TemplateItem $sliderDataFolderTemplate

which results in:

Set-ItemTemplate : Object reference not set to an instance of an object.
At line:4 char:1
+ Set-ItemTemplate -ID $item.ID -TemplateItem $sliderDataFolderTemplate
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Set-ItemTemplate], NullReferenceException
    + FullyQualifiedErrorId : System.NullReferenceException,Cognifide.PowerShell.Commandlets.Data.SetItemTemplateCommand

I tried wrapping Set-ItemTemplate with an EventDisabler context but that did not fix the problem.

New-UsingBlock (New-Object Sitecore.Data.Events.EventDisabler) {     
  Set-ItemTemplate -ID $item.ID -TemplateItem $sliderDataFolderTemplate

1 Answer 1


Not the nicest way but you may try to call TemplateManager.ChangeTemplate method:

$newTemplateIdString = "{76036F5E-CBCE-46D1-AF0A-4143F9B557AA}";
$itemIdString = "{A3FEC1DD-813C-44BD-9A37-66F0FADFD32F}"

$db = [Sitecore.Data.Database]::GetDatabase("master");
$templateId = [Sitecore.Data.ID]::Parse($newTemplateIdString)
$template = [Sitecore.Data.Managers.TemplateManager]::GetTemplate($templateId, $db)
$item = $db.GetItem($itemIdString)

$parameters = @{
    TypeName = 'Sitecore.Data.Templates.TemplateChangeList'
    ArgumentList = ($template, $template)

$templateChangeList = New-Object @parameters

[Sitecore.Data.Managers.TemplateManager]::ChangeTemplate($item, $templateChangeList)
  • It might not be the nicest way, but it works! Thank you! Sep 13, 2019 at 11:14

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.