Question:

Migrating values of a field to another field

Caleb: 2 weeks ago

I have a base template with property 'Intro Text' of type multiline text. This base template is used by 4 different templates. Now, one of these templates needs this field as a RTE field. But I can't change the field-type in the base template, because that would break the other components. So I am thinking to create a new field 'Intro Text New' in the template that needs the RTE and I would need to copy all the existing content from the field 'Intro Text' and remove the base template from this template afterwards. Is there a way to migrate field values from one field to another in Sitecore? Also, would there be a better/faster way to solve the problem above?

Answer:
Maverick: 2 weeks ago

Creating a new field on the template that needs the RTE should work. In order to automate you could use Sitecore Powershell Extensions, something like the following should work (I've not had chance to test this yet):

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

Get-ChildItem $rootItem.FullPath -Recurse | Where-Object { $_.TemplateName -eq $sourceTemplate.Name } | ForEach-Object {  
        $_.Editing.BeginEdit()
        $fieldValue = $_.Fields["Intro Text"].Value
        $_.Fields["Intro Text New"].Value = $fieldValue;
        $_.Editing.EndEdit()
}

Corey wrote a useful blog post: https://blogs.perficient.com/sitecore/2016/06/13/sitecore-powershell-change-item-template-and-retain-field-value/ on copying from one template to another and updating fields which I've based this on. Another useful reference here too: https://therelentlessfrontend.com/2017/11/14/sitecore-powershell-to-update-item-fields/

Also I believe Sitecore Rocks allows you to do this using an update query:

update set @#Intro Text New# = '' + @#Intro Text# from /sitecore/content//*[@@templatename = 'Promo'];

One thing to consider though is that if on the template you add the 'Intro Text New' field to you leave the base-template in place you will end up with two fields which may be confusing for content editors so perhaps look at re-organising the base template this is using afterwards etc.