Tip: Sitecore MVC data source nesting – gotcha!

The current project I’m working on is using Sitecore 8 with MVC. I recently ran into an error when I added a rendering into a placeholder, on another rendering, via the Experience editor. Now this post isn’t a slap on the wrist for my rendering not being robust enough 😉 but actually highlighted something interesting. The reason my rendering was throwing an exception was because the context item for the rendering was not the Sitecore.Context.Item. Confused by this scenario, given I’d only just added the rendering and therefore no data source had been set, I investigated.

Quite quickly it was clear what was happening. The rendering I was adding was in fact reading the data source from the parent rendering rather than the context item. This was behaviour that I wasn’t expecting! Thankfully Sitecore support came to the rescue and informed me of a setting which can be toggled to tell MVC renderings whether or not to nest data sources.

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
      <!--  MVC: Flag for controlling data source nesting.
            Default: "true"
      <setting name="Mvc.AllowDataSourceNesting" value="true"/>

If you’re always expecting your renderings to read from the context item (and you’re using MVC) then you should set the above to “false”.

Thank you Sitecore support for this.

Jason Bert