Creating SXA Variant Components that Work With your Own Templates

If you look at SXA components that support Variants like "Page Content" and "Promo" You'll notice some of them actually point to the same generic controller: Sitecore.XA.Foundation.RenderingVariants.Controllers.VariantsController

Promo Rendering.png

If you want to create your own version of the promo and use Variants to define the presentation, you may think you just need to copy this rendering definition and change the data source template and location, but this unfortunately doesn't work. You will get an error saying "An unhandled exception occurred in RenderingVariants Action: Index.

Promo Copy Error.png

If you look in your Sitecore logs, you'll see the real error:

Exception: System.InvalidOperationException
Message: The partial view 'Promo Copy' was not found or no view engine supports the searched locations. The following locations were searched:

This is because the default behavior of this controller is to look for a view definition with the same name as the component. If you copy and rename the promo component view in the "Variants" folder, it will work. It just adds an extra step that doesn't have a lot of value.

Instead I created my own controller that follows the same Rendering Variant approach as the promo component, but always resolved the view to the same file regardless of which component is calling it. It's now part of the SXA branch of Sitecore Foundation, so if you're using it you can just point your controller to: Sitecore.XA.Foundation.RenderingVariants.Controllers.VariantsController, Sitecore.XA.Foundation.RenderingVariants with an Action of Index and things will just work.

Sitecore Foundation - Extending Rendering Variant Fields to Support Handlebar Templates