Change Data template for Sitecore Items while retaining data for existing fields

Recently, had to move a bucket of items from one template to another. I also thought this would be a classic case to showcase a scenario to migrate data from source to target fields since most of the fields were existing ones with just field name change. 

Source and target fields:

Source Field

Source Field Display Name

Target Field

Target Field Display Name

Field Type

Is Active

Is Active

IsActive

Is Active

 Checkbox

Is Shown in Special Offers

Is Shown in Special Offers

IsShowninCurrentOffers

Is Showin in Current Offers

 Checkbox

Regions

Regions

Regions

Regions

 treelist

Disclaimers

Disclaimers

Disclaimers

Disclaimers

 treelist

CTAs

CTAs

CTAs

CTAs

 treelist

Offer Price

Offer Price

OfferPrice

Offer Price

 Number

Sub Title

Sub Title

VehicleSubtitle

Vehicle Sub-title

Multiline text

Title

Title

VehicleTitle

Vehicle Title

Singleline text

Is Non Price Offer

Is Non-Price Offer

IsNonPriceOffer

Is Non-Price Offer

Checkbox

Material Codes

Material Code(s)

MaterialCodes

Material Code(s)

treelist


I just have a snapshot of the fields here in the table above. Even in the snapshot above, it can be noticed that some of the fields like "Is Active" if named without any spaces, there is no need to include them in the migration PS script (below) since they would map automatically and port data. 

So, It will be good to have Item names without spaces. Instead, go with the age-old convention of adding special characters and spaces to display name field since that is the reason for having the display name field. While item name without spaces can be useful while writing scripts.

Also, note that in the PS script below, the item names are NOT display names but the actual item names. So, ensure to pick up the item name when you write such scripts.

Finally, here is my PS script that changes the template and migrates the data:

########################################################

Write-Host "Start"

########################################################

$rootItem = Get-Item master:"/content/xyz/Main Site/test";

$sourceTemplate = Get-Item "{89FA35EA-F1DF-49DD-A429-FC39D015500C}";

$targetTemplate = Get-Item "{7977385E-3F2C-4FC3-9502-A95B43EF9A35}";

Get-ChildItem $rootItem.FullPath -recurse | Where-Object { $_.TemplateName -eq $sourceTemplate.Name } | ForEach-Object {

    $fieldVehicleGrade= $_."Vehicle Grade";

    $fieldAvailableExtras= $_."Available Extras";

    $fieldKeySellingFactors= $_."Key Selling Factors";

    $fieldVehicleVariant= $_."Vehicle Variant";

    $fieldMaterialCodes= $_."Material Codes";

    $fieldImageAlt= $_."Image Alt";

    $fieldOfferPrice= $_."Offer Price";

    $fieldTitle = $_."Title";

    $fieldSubTitle = $_."Sub Title";

    $fieldOverrideOfferPrice= $_."Override Offer Price";

    $fieldEnableOmnichannelIntegration= $_."Enable Omnichannel Integration";

    $fieldIsNonPriceOffer= $_."Is Non Price Offer";

    $fieldIsActive = $_."Is Active";

    $fieldTitle = $_."Title";

    $fieldSubTitle = $_."Sub Title";

    $fieldIsShowninSplOffers= $_."Is Shown in Special Offers";

    $fieldIsShownonPricingPage= $_."Is Shown on Pricing Page";    

    $_.ChangeTemplate($targetTemplate);

    $updatedItem = Get-Item $_.ID;    

    $updatedItem.VehicleTitle = $fieldTitle;    

    $updatedItem.VehicleSubTitle = $fieldSubTitle;    

    $updatedItem.VehicleGrade = $fieldVehicleGrade;

    $updatedItem.AvailableExtras = $fieldAvailableExtras;    

    $updatedItem.VehicleInclusions = $fieldKeySellingFactors;    

    $updatedItem.VehicleVariant = $fieldVehicleVariant;    

    $updatedItem.VehicleGrade = $fieldVehicleGrade;    

    $updatedItem.MaterialCodes = $fieldMaterialCodes;    

    $updatedItem.ImageAlt = $fieldImageAlt;    

    $updatedItem.OfferPrice = $fieldOfferPrice;    

    $updatedItem.IsNonPriceOffer = $fieldIsNonPriceOffer;    

    $updatedItem.OverrideOfferPrice = $fieldOverrideOfferPrice;    

    $updatedItem.VehicleVariant = $fieldVehicleVariant;    

    $updatedItem.IsShowninCurrentOffers = $fieldIsShowninSplOffers;    

    $updatedItem.IsShownonPricingrelatedModules = $fieldIsShownonPricingPage;    

    $updatedItem.IsActive = $fieldIsActive;

}

Write-Host "Done"

###############################################################

Also, notice how easy it is to add field names without spaces to the target $updatedItem variable in the above script. Also, on the right side, you can see the source field with spaces enclosed within double quotes. 

Most importantly, ensure to do an iisreset before running the script!

Comments