Sitecore Powershell script to change template of items

Pranay    08 May '19    72 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.

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

About the author

Pranay Deegoju
Sitecore Certified Professional

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



Leave your comments on this post here


Want more such posts, subscribe here

Recent Posts

×

Liked this article?

Subscribe with your email and get updates when ever a new article is posted.

By subscribing you agree to our privacy terms. We promise that we do not sell your data.