I'm copying a rendering from one item to another using Sitecore Powershell Extensions.
All is working well, except that the personalization rules for the rendering aren't copied over.
I can get/set the rules on a rendering definition item without any error, but both Add-Rendering and Set-Rendering seem to ignore any rules on the rendering definition.
Example code that I've tried without success:
$sourceRendering = Get-Rendering -Item $Source -FinalLayout | Where-Object{$_.uniqueid -eq "{a unique id}"}
This works to get the rules, and the output has content:
$sourceRenderingRules = $sourceRendering.Rules
Add the rendering to a different item:
Add-Rendering -Item $TargetItem -Instance $sourceRendering -PlaceHolder $sourceRendering.placeholder -FinalLayout
Retrieve the new rendering (it will have a new uinique ID, so get the right one in code not shown or use a different way of selecting it)
$CopiedRendering = Get-Rendering -Item $Target -FinalLayout | Where-Object{$_.uniqueid -eq "{a unique id}"}
No error, but the Rules are empty
$CopiedRenderingRules = $CopiedRendering.Rules
Copy rules to the copied rendering, no error
$CopiedRendering.Rules = $sourceRenderingRules
Copied rendering now has rules, output has content
$CopiedRenderingRules = $CopiedRendering.Rules
Update the rendering
Set-Rendering -Item $TargetItem -Instance $CopiedRendering -PlaceHolder $CopiedRendering.placeholder -FinalLayout
When I then go back and get the rendering from the target, the rules are empty.
Have I missed something? Or will I have to work with the XML in the final layout field directly?