1

I have a Sitecore PowerShell script and I am using it to insert a rendering at a particular position on the page.

To do this, I am using the [-Index <Int32>] parameter on the Add-Rendering command.

Add-Rendering -Item $newItemVersion -Rendering $legacyRow -Placeholder "page-body" -FinalLayout -Index 50

You can see the full script here for more detailed look. Basically, I am looping over a number of items that all need to be updated. Each item has a parent row rendering added.

The problem I encounter is that an Index of 50 works (a number that seemed to work in my test) on one item but not for others.

On other items, the script throws an error:

Add-Rendering : Insertion index was out of range. Must be non-negative and less than or equal to size.

Does anyone know, how I properly calculate the Index parameter so that it does not exceed the maximum?

1 Answer 1

3

The -Index property dictates the index of all of the renderings for a specific device. Much like an array, you won't be able to go out of bounds. If you have an item with 51 renderings in your presentation details for a specific device, an index of 50 will work. However, if you have less, you'll get the exception that you just got as your index of 50 won't exist in a smaller-sized rendering collection.

If you want to append the new rendering to the end, you can leave off the -Index property altogether and SPE will insert it at the end.

This is different than the SortOrder field in Sitecore items which takes in an arbitrary number and sorts according to that value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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