We are trying to refactor few of the Sitecore templates. We need some help with powershell script. Is there any way to pull bunch of fields (around 70-80) to different template sections. Manual pull is taking lots of time. Can we do this through powershell?
1 Answer
Let's say you start with a template like the following:
Then the following script will move items to a newly created section like this:
Using this sample, you can build in whatever logic you want. Keep in mind that if you are wanting to move fields between templates then you need to change the inheritance first.
# Get the template I want to change
$templateId = "{F8BC7158-5A2F-40FE-A7D9-453229EC325E}"
$selectedTemplate = Get-Item -Path "master:" -ID $templateId
$currentChildren = $selectedTemplate.Children
$count = $currentChildren.Count + 1
# Add a new template section
$templateSectionId = "{E269FBB5-3750-427A-9149-7AA950B49301}"
$newSection = New-Item -Parent $selectedTemplate -Name "Section$($count)" -ItemType $templateSectionId
# Move fields
foreach($currentChild in $currentChildren) {
$child = $currentChild.Children[0] | Initialize-Item
Write-Host "Moving $($child.ItemPath)"
Move-Item -Path $child.ItemPath -Destination $newSection.ItemPath
}