Changing Sitecore Item Data Templates with SPE

spe icon This week I needed to change a number of navigation items from an old navigation item data template to a new one. I didn’t want to have to do this by hand as I have quite a few items to update, so I decided that Sitecore Powershell Extensions (SPE) was the quickest and easiest way to do this. If you don’t know about SPE I’ve written more about it here.

There isn’t an function as such to do this in SPE so I put one together and thought I’d share it here.

How do I use it?

The function updates the data template of an item (and optionally it’s child items) from one template to another. It has 4 parameters:

  • RootItemId – Specifies an Item Id to start searching for items from
  • SourceTemplateId – Specifies the Id of the template to match items on (the template you wish to change)
  • TargetTemplateId – Specifies the Id of the template to change items to
  • Recurse – Specifies if all the child items below RootItemId should be included in the search.

An Example

Change-Template -RootItemId 36584658-7B62-4572-932E-8214A3CD7CE8 -SourceTemplateId 92C1C171-3ED0-4E8F-860A-FEAF14336A96 -TargetTemplateId DD68EBBB-4D82-411C-BA19-D95A65AAEF83 –Recurse $true

Change-Template Function

Hopefully this is useful for others who need to do something similar.

Potential Improvements / Alternatives

If you have an number of templates that you want to update in various locations in your Sitecore solution you could use the Get-ItemReferrer option before the | Where-Object query and this will get all items referencing the template instead of searching the entire Sitecore content tree. In my case however I didn’t want to update all items with old template, just ones under a specific path.

I could perhaps update my function at some point to accept a $UseGetItemReferer boolean option and also allow for a null $RootItemId in order to support this and make the function more flexible.