3

The layout of our website has been completely overhauled. The old placeholders didn't make sense anymore, so they aren't used in the new layouts. As a result, most content pages are now empty. All pages still have controls set up in the presentation details, they are just not shown since they are mapped to old placeholders.

What I need to do is go through all pages, take all controls mapped to non-existing placeholders in their current page layout, and move them to the "content" placeholder. This way, content editors will be able to see these controls in the Experience Editor and move them to other placeholders on the page.

Note that some pages have already been worked on, so the controls that are mapped to valid placeholders should stay where they are.

Is there an automated way of doing this? An SPE script, perhaps?

3

We had to do a similar job a while ago. We decided not to go for SPE this time as there was quite some logic involved in the placeholder replacement - we decided to write a custom page that starts a job in a long-running thread (as we had to go over a lot of items).

I assume you know how to loop over all (needed) items, so I'll cover 1 item. For the replacement itself we had 2 options:

  • use the item.Visualization.GetRenderings, loop over all renderings and set the Placeholder value to "content" (if needed)
  • edit the __Renderings field of your item straight away (the placeholder names are in the xml as strings, so you could replace them)

We edited the __Renderings field (because we could and it saved us a loop).

This will only work if you know which placeholders to replace and which to keep. If you don't and need to get the available placeholders first (available is not same as used - "used" placeholders is easy to get from all renderings), your code would become a lot more difficult.. there is a blog here that describes how you could do that but I never tried it (and it seems to be webforms).

If you would do it this way, add logging so you know what is happening ;)

  • Is there a programmatic way to find out what placeholders are available for a given page in its layout? – Dmytro Shevchenko Nov 22 '16 at 12:25
  • 1
    What could be an option to have the "available placeholders" is that you create a collection with layouts and available placeholders once in code (manually - so only if you don't have too many). – Gatogordo Nov 22 '16 at 12:29
4

Yes, you could use this script.

$item = Get-Item -Path '/sitecore/content/Home/SomeItem'

Get-Rendering -Item $item | Foreach-Object { 
   $_.Placeholder = "content"; 
   Set-Rendering -Item $item -Instance $_
}

The second line is the important part. It takes each rendering and sets the placeholder key to "content"

See for review: https://sitecorepowershell.gitbooks.io/sitecore-powershell-extensions/content/appendix/commands/Set-Rendering.html

Edited: Update multiple items with a given placeholder key

$startPath = "/sitecore/content/Home"
$oldPlaceholder = "some_placeholder"
$newPlaceholder = "content"

Get-ChildItem -Path $startPath -Recurse | ForEach-Object { 
   $item = $_;

   Get-Rendering -Item $item -PlaceHolder $oldPlaceholder | Foreach-Object { 
      $_.Placeholder = $newPlaceholder; 
      Set-Rendering -Item $item -Instance $_
   }
}
  • 1) I need to do this for all items; 2) I want to change control placeholders only if the placeholder doesn't exist in the current page layout. – Dmytro Shevchenko Nov 22 '16 at 10:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.