Pages

04 June, 2021

Sitecore RenderingContext.Current.Rendering.Item changed? - Sitecore 9 vs Sitecore 10

As part of  Sitecore 9 to Sitecore 10 upgrade, few components did not work after the upgrade and it was related to the rendering datasource.

Problem: I have a datasource on an ancestor Controller rendering. Child Controller rendering does not have a datasource. Child Controller rendering uses ancestor rendering datasource Item to render the data. After the Sitecore 10 upgrade, Child Controller rendering couldn't get the ancestor datasource item using RenderingContext.Current.Rendering.Item and instead it returned page content Item. 

There is a great blog on Sitecore MVC Rendering datasource topic by Matthew Dresser. He explains about RenderingContext.Current.Rendering.Item clearly. It helps to "get datasource of the current rendering, or the datasource of an ancestor rendering if specified, or the page item if no datasource set and no ancestor rendering with datasource. Rendering datasource takes priority". Datasource of an ancestor rendering will be returned if the setting Mvc.AllowDataSourceNesting is true. It is true by default.

After the upgrade, RenderingContext.Current.Rendering.Item does not return ancestor rendering datasource and instead it returns Page context item. 

Raised a Sitecore Support ticket whether it is an expected change or a bug. Will update once I hear from them.

As workaround, I wanted it to behave in the same fashion to reduce the impact. I added a wrapper method which gives priority to child rendering datasource if available, then ancestor rendering datasource if available and then page context item. 

References:

No comments:

Post a Comment