LibrarySites.Banner

RenderAddedContent NullReferenceException for Item Controller in the Sitecore ASP.NET CMS

This blog post describes a solution to a NullReferenceException that I experienced in the MVC controller specified for an item in version 8 of the Sitecore ASP.NET web Content Management System (CMS) and Experience Platform (XP).

A Sitecore solution can handle a URL in several ways, such as applying the media request handler or rendering the page with ASP.NET Web Forms.

In my scenario, URLs map to data items in the content management system. In the Layout section, those items specify both layout details and a value for the Controller field. While layout details must specify a layout, Sitecore invokes an action in the specified controller. That action can invoke View() to return a view. If that view contains a placeholder, for example by invoking the @Html.Sitecore().Placeholder() method, Sitecore applies the renderings specified in layout details to the view determined by the call to the View() method.

In Sitecore version 8.141212 and possibly other versions, this scenario may result in a NullReferenceException from the RenderAddedContent processor in the mvc.renderPlaceholder pipeline.

namespace SitecoreJohn.Mvc.Pipelines.Response.RenderPlaceholder
{
  public class RenderAddedContent : Sitecore.Mvc.Pipelines.Response.RenderPlaceholder.RenderAddedContent
  {
    public override void Process(Sitecore.Mvc.Pipelines.Response.RenderPlaceholder.RenderPlaceholderArgs args)
    {
      if (args != null && args.OwnerRendering != null && args.OwnerRendering.Properties != null)
      {
        base.Process(args);
      }
    }
  }
}

The name of the web.config include file should follow Sitecore.Speak.Mvc.config alphabetically.

<configuration xmlns:patch="https://www.sitecore.com/xmlconfig/">
  <sitecore>
    <pipelines>
      <mvc.renderPlaceholder>
        <processor type="Sitecore.Mvc.Pipelines.Response.RenderPlaceholder.RenderAddedContent, Sitecore.Speak.Mvc">
          <patch:attribute name="type">SitecoreJohn.Mvc.Pipelines.Response.RenderPlaceholder.RenderAddedContent,SitecoreJohn</patch:attribute>
        </processor>
      </mvc.renderPlaceholder>
    </pipelines>
  </sitecore>
</configuration>
  • This didn't work for me without:

    1) I needed to reference Sitecore.Speak.Mvc.dll

    2) I needed to add xmlns:set="www.sitecore.net/.../"

    <configuration xmlns:patch="www.sitecore.net/.../" xmlns:set="www.sitecore.net/.../">

     <sitecore>

       <pipelines>

         <mvc.renderPlaceholder>

           <processor type="Sitecore.Mvc.Pipelines.Response.RenderPlaceholder.RenderAddedContent, Sitecore.Speak.Mvc">

             <patch:attribute name="type">SitecoreJohn.Mvc.Pipelines.Response.RenderPlaceholder.RenderAddedContent,SitecoreJohn</patch:attribute>

           </processor>

         </mvc.renderPlaceholder>

       </pipelines>

     </sitecore>

    </configuration>

  • Thanks John, this worked perfectly with my small additions