LibrarySites.Banner

Conditional Rendering in MVC Solutions That Use the Sitecore ASP.NET CMS

This blog post attempts to address some confusion about support for conditional renderings in MVC solutions that use the Sitecore ASP.NET web Content Management System (CMS). Specifically, Sitecore MVC solutions provide support for personalization and testing, which are forms of conditional rendering.

The statement "Conditional Renderings are not supported in MVC" in the current version of the Sitecore MVC Developer’s Reference Guide is certainly misleading, and the documentation team is already aware of this issue. Sitecore personalization and testing features depend on the conditional rendering engine, and Sitecore MVC solutions support those features. Sitecore MVC solutions do not support global conditional rendering rules, which is a form of conditional rendering that applies the same set of rules to all presentation components. You can apply conditional rendering rules for individual presentation components, but you cannot define conditional rendering rules that apply to all presentation components as you can with Web Forms solutions.

I did not check whether you need a DMS license to apply multivariate tests, as I expect anyone performing Sitecore multivariate testing would use DMS to store the results. You do not need a DMS license to use the rules engine for personalization, which means you can use the rules engine to apply any type of conditional rendering. You may need a DMS license to use conditions and actions specific to DMS, such as anything that depends on DMS GeoIP logic.

It should be possible for you to implement global conditional rendering rules for MVC solutions, but it could be a bad idea. Specifically, you could implement a processor similar to the Personalize processor that the /App_Config/Include/Sitecore.MvcExperienceEditor.config file adds to the mvc.customizeRendering pipeline with logic similar to the EvaluateConditions processor in the insertRenderings pipeline defined in the /web.config file.

But there is probably a reason that Sitecore did not do that. I assume that Sitecore still supports global conditional rendering rules for Web Forms, but I would avoid using this feature, and Sitecore may eventually stop supporting its use. Regardless of how efficient such logic may be, performing some operation each time you bind a rendering to a placeholder seems excessive unless that logic really applies to all components. It would be preferable to apply the logic within the component, or at least to specific types of component such as views or controllers. There are probably more efficient ways to implement the same requirement without global conditional rendering rules.

Resources

  • Thanks for clarifying John. Was still wondering about this.  RuSs

  • Dear John,  I can't make conditional renderings working with MVC. Is there any examples of doing so?

  • Hello Maksym,  Do you mean that the relevant features do not appear in the Page Editor or elsewhere, or that they do not apply at runtime?  Do you mean global conditional rendering rules, which Sitecore may have obsoleted (at least for MVC), or conditional rendering rules specific to individual rendering components?  If not global conditional rendering rules, what types of components, and what conditions and actions have you tried to use?

  • Hi John,  I use Sitecore 7.0 MVC. I also could not make conditional rendering working with MVC. Here are the following steps: - I created a few Controller rendering components with controller actions generating different MVC Razor views and single model.  -  I added custom condition using my .NET class and added rules using this custom condition in Marketing Center.  - When I configured the Presentation Details layout, these rules are actually available in the Personalization section of  Controller rendering components configured in the Layout details dialog and I checked the rules appropriate with the components. - I also added some diagnostic trace in the custom condition .NET class.  - When I loaded the page in Page Editor, all Rendering components are shown (instead of conditionally). When I activated Trace output, I do not see the custom condition being executed at all (i.e. no trace output from my custom condition).  

  • Could you please tell alternatives to global condition rules in sitecore MVC?