26

We are refactoring our project and are trying to clean up the usages of templates throughout the project. There are a number of templates that are very similar that could all be using the same data template, which would also mean we can better utilize the "Compatible Renderings" functionality in Sitecore making it easier for editors to change components.

The problem is we have existing content and it would be a pain have to re-create all the datasources and copy across the data manually.

For example, given the following data templates:

Template A

Title1        Single-Line Text
SubText1      Multi-Line Text
Body1         Rich Text
Image1        Image

Template B

Title2        Single-Line Text
SubText2      Rich Text
Body2         Rich Text

How can we change all items which use Template B to instead use Template A and still retain all existing data?

22

You can do this with Sitecore PowerShell Extensions.

PowerShell Extensions 4.5 or Higher

Use the Set-ItemTemplate commandlet with the -FieldsToCopy parameter.

$rootItem = Get-Item master:/content;
$sourceTemplate = Get-Item "Template-B-Path-Or-Guid-Here";
$targetTemplate = Get-Item "Template-A-Path-Or-Guid-Here";

Get-ChildItem $rootItem.FullPath -recurse | Where-Object { $_.TemplateName -eq $sourceTemplate.Name } | ForEach-Object {
    Set-ItemTemplate -Item $_ -TemplateItem $targetTemplate `
        -FieldsToCopy @{ Title2 = "Title1"; SubText2 = "SubText1"; Body2 = "Body1" }
}

The value you pass to the -FieldsToCopy parameter takes the following form: @{ OldField1 = "NewField1"; OldField2 = "NewField2"; OldField3 = "NewField3" }.

PowerShell Extensions 4.4.1 or Lower

$rootItem = Get-Item master:/content;
$sourceTemplate = Get-Item "Template-B-Path-Or-Guid-Here";
$targetTemplate = Get-Item "Template-A-Path-Or-Guid-Here";

Get-ChildItem $rootItem.FullPath -recurse | Where-Object { $_.TemplateName -eq $sourceTemplate.Name } | ForEach-Object {
    $title2 = $_.Title2;
    $subText2 = $_.SubText2;
    $body2 = $_.Body2;

    $_.ChangeTemplate($targetTemplate);
    $updatedItem = Get-Item $_.ID;

    $updatedItem.Title1 = $title2;
    $updatedItem.SubText1 = $subText2;
    $updatedItem.Body1 = $body2;
}

With PowerShell Extensions 4.4.1 or lower, be careful that you do the following or you may experience data loss:

  1. Store the source field value into a variable before you change the item’s template.
  2. Get the updated item out of the database before you set the target field.
| improve this answer | |
18

You can rename Title2 to match name of Title1 and then simply change the template.

Sitecore will preserve data if the names of the fields match.

Template A

Title1        Single-Line Text
SubText1      Multi-Line Text
Body1         Rich Text
Image1        Image

Template B

Title2 -> Title1        Single-Line Text
SubText2 ->SubText1      Rich Text
Body2 -> Body1         Rich Text

After changes in Template B, you can change Template B to Template A and Sitecore will preserve the data

| improve this answer | |
  • 1
    This is wrong. Sitecore does not remap fields. – Mark Cassidy Oct 11 '16 at 1:07
  • Hmmm, are you sure? I checked that before posting. When the names of fields are different I am getting a warning that data migh get lost but when I change the name of the field to match original template Sitecore allows to change without warnings and the data stays. – Szymon Kuzniak Oct 11 '16 at 6:00
  • Are you sure it's not because you're testing on templates that inherit common base templates? Data will stay if Field IDs stay the same. – Mark Cassidy Oct 11 '16 at 7:13
  • 3
    @MarkCassidy: The fields come from different templates (both inherit from Standard Template). You might want to check GetTemplateChangeList method from the Sitecore.Data.Templates.Template class - you can see that if the IDs of the fields are different Sitecore matches fields by name (object created there is then further passed to ChangeTemplate method). – Szymon Kuzniak Oct 11 '16 at 8:01
  • 1
    This worked for me on sitecore 8.1 update 3, SPE had the advantage of also being able to change template types of existing items – jammykam Oct 12 '16 at 23:59
5

You can try to execute SQL script which will do the trick for you and update all the items which use particular template.

First you need to replace the field ids used in your original template with field ids used in the new template and then change the template of the item itself.

I haven't tested the script so make sure you have backups.

UPDATE VersionedFields SET [FieldId] = 'NEW_FIELD_ID' WHERE [FieldId] = 'OLD_FIELD_ID';
UPDATE UnversionedFields SET [FieldId] = 'NEW_FIELD_ID' WHERE [FieldId] = 'OLD_FIELD_ID';
UPDATE SharedFields SET [FieldId] = 'NEW_FIELD_ID' WHERE [FieldId] = 'OLD_FIELD_ID';
UPDATE Items SET [TemplateID] = 'NEW_TEMPLATE_ID' WHERE [TemplateID] = 'OLD_TEMPLATE_ID';

After the script is finished, restart the application and republished the site.

| improve this answer | |
  • Seems like you will lose your data if swtiching field types (versioned/unversioned/shared). Like if you're converting from the Unversioned/PDF to Versioned/PDF... the data will not have moved from one table to another – MStodd Apr 27 at 17:51
  • @MStodd that's obvious. You cannot change type of the field like that. – Marek Musielak Apr 28 at 11:00
5

The Sitecore Rocks extension for VS allows you to select multiple items at once and change their template.

enter image description here

This is useful if all of the items, you want to change the template of, are in the same place.

UPDATE:

If the field names of the templates are different, you can change them on the new template to match the old one's, change the template of the items, and then change the field names back to the original ones.

| improve this answer | |
  • 1
    This is only useful for local or instances which we can connect to using Rocks. Just changing the template means we will lose data. – jammykam Oct 10 '16 at 10:24
  • That's true. You could however change the field names of the new template to match the old one's, change the template of the items, and then change the field names back to the original ones. – adragomanov Oct 12 '16 at 14:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.