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:

3 comments:

  1. Did you hear back from sitecore? I've got the same problem. Looks like a bunch of code in my solution relies on this fallback resolution so that RenderingContext.Current.Rendering.Item is never null, but with the upgrade to 10.1 I'm getting NullReferenceExceptions all over the place due to the item resolution not working. ARRGH

    ReplyDelete
    Replies
    1. I got to know from Sitecore that they have changed the behavior in Sitecore 10. Sitecore support did not register this behavior as a bug.

      We used Glass controller in Sitecore 9 and it has been removed in Glass SC 10 version. So anyway we had to refractor our code to accommodate this change.

      Delete
  2. To add to my previous comment... the problem seems to be exacerbated by another behavioural change. In previous Sitecore versions (just upgraded from 8.2 to 10.1) a controller rendering simply would not trigger the controller action if the datasource field was not empty but the item it referred to couldn't be found. E.g. an unpublished item which exists on master but not web. Old sitecore totally skips the attempt to render the view - no controller action is executed. New sitecore goes ahead and runs the controller action. Then when I try to get RenderingContext.Current.Rendering.Item it's null, and code that expects it to be not-null falls over. I guess on the plus side an exception is better than the documented behaviour of falling back to ancestor item and making me wonder why random stuff is showing up ��

    ReplyDelete

blockquote { margin: 0; } blockquote p { padding: 15px; background: #eee; border-radius: 5px; } blockquote p::before { content: '\201C'; } blockquote p::after { content: '\201D'; }