Sitecore Powershell — Converting Items
I’m working on a project that involves a large-scale refactor of a Sitecore application. Most of the Templates and Items from the previous version of the site have been copied over to the new installation, but there was one section of the site that needed to be rebuilt from scratch. How do we convert the legacy items into the new types? Sitecore Powershell to the rescue!
This is going to be a full conversion of old data, so anything in the destination location needs to be deleted first.
$destFolder = Get-Item -Path "master://sitecore/content/NewItems"
$existing = Get-ChildItem -Path $destFolder.ItemPath
ForEach ($exists in $existing) {
Write-Host "Removing Item:" $exists.ItemPath
Remove-Item -Path $exists.ItemPath -Permanently
}
Once the destination is clear, we can create the new items and migrate the data over.
$oldItems = Get-ChildItem -Path "master://sitecore/content/OldItems"
ForEach ($oldItem in $oldItems) {
$newItem = New-Item -Parent $destFolder -Name $oldItem.Name
-ItemType "/sitecore/templates/Feature/MyFeature/NewItem"
$newItem.Editing.BeginEdit()
$newItem["Name"] = $oldItem["Item Name"]
$newItem["Description"] = $oldCategory["Item Text"]
$newItem.Editing.EndEdit()
Write-Host “New Item Created:” $newItem["Name"]
}
With items as simple as this example, changing the field names and template would have been a better solution, but scripts like this one allow you to perform more complex conversions.