Sitecore Powershell script to change template of items

Pranay
May 16, 2020  ยท  6705 views

This post would help you to change the template of some items programatically with Sitecore Powershellscript to a new template. It would actually change the templateId of the item using Powershellscript.

If you see a template is not available but there are items which are using this template then it is not possible to even select the item. Clicking on the item throws an error. In such cases the only possible option is to change the template of those items programatically. Here in this post we will have a Sitecore Powershell script to get all such items and change the template of the item to something that is available. This will make the item accessible and also possible to edit or change anything related to the item.

Is this not the script you are looking for? Then check the complete list of Sitecore Powershell scripts

Sitecore Powershell script to change template of selected items

$props = @{
   InfoTitle = "Change Template of items"
    PageSize = 10000000
}

$sourcePath = "master:/sitecore/content/Home/Site2"
$items = Get-ChildItem -Path $sourcePath -Recurse
#Uncomment below two lines if you want to include the root item too.
#$rootItem = Get-Item -Path $sourcePath
#$items = $items + $rootItem

function Change-Template {

    # Logic
    foreach($item in $items)
    {
        if ($item.TemplateId -eq "{BAAF9E6D-000F-4C58-9BDF-E0E6F04EF5A9}")
        {
            $item.Editing.BeginEdit();
            $item.TemplateId = "{8DCF4868-F8F5-4313-86E8-EE1E4CBF0B13}"
            $item.Editing.EndEdit();
            Write-Host "Item id: " $item.ID " - Template id: " $item.TemplateId
        }
    }

}

$items = Change-Template
$items | Show-ListView @props -Property ItemPath, ID, @{Label="Language"; Expression={$_."Language"}} 
Close-Window

Powershell script change template

AUTHOR

Pranay

A Software Engineer by profession, a part time blogger and an enthusiast programmer. You can find more about me here.


Post a comment




Thank you! You are now subscribed.

Sign up for our newsletter

Subscribe to receive updates on our latest posts.

Thank you! You are now subscribed.