1

I am trying to build my own SXA custom rendering with a variant by following the articles from Sitecore:

https://doc.sitecore.net/sitecore_experience_accelerator/building_the_layout/renderings/walkthrough_building_a_new_rendering

https://doc.sitecore.net/sitecore_experience_accelerator/setting_up_and_configuring/configuring/build_a_rendering_that_includes_variants#_Repository

However, when I tried adding it to my page via experience editor, it appears that I am doing something wrong.

Screenshot: enter image description here

Stack Trace

 at Sitecore.Mvc.Pipelines.MvcEvents.Exception.ShowAspNetErrorMessage.ShowErrorMessage(ExceptionContext exceptionContext, ExceptionArgs args)
   at Sitecore.Mvc.Pipelines.MvcEvents.Exception.ShowAspNetErrorMessage.Process(ExceptionArgs args)
   at (Object , Object[] )
   at Sitecore.Pipelines.CorePipeline.Run(PipelineArgs args)
   at Sitecore.Pipelines.DefaultCorePipelineManager.Run(String pipelineName, PipelineArgs args, String pipelineDomain)
   at Sitecore.Mvc.Pipelines.PipelineService.RunPipeline[TArgs](String pipelineName, TArgs args)
   at Sitecore.Mvc.Filters.PipelineBasedRequestFilter.OnException(ExceptionContext exceptionContext)
   at System.Web.Mvc.ControllerActionInvoker.InvokeExceptionFilters(ControllerContext controllerContext, IList`1 filters, Exception exception)
   at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)
   at System.Web.Mvc.Controller.ExecuteCore()
   at System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext)
   at Sitecore.Mvc.Controllers.ControllerRunner.ExecuteController(Controller controller)
   at Sitecore.Mvc.Controllers.ControllerRunner.Execute()
   at Sitecore.Mvc.Presentation.ControllerRenderer.Render(TextWriter writer)
   at Sitecore.Mvc.Pipelines.Response.RenderRendering.ExecuteRenderer.Render(Renderer renderer, TextWriter writer, RenderRenderingArgs args)

I have built the controller, repository, view and register it as dependency - backend codes are shown below.

Controller\DCardsController.cs

using PROJECTABC.Feature.Composites.SXA.Repositories;
using Microsoft.Extensions.DependencyInjection;
using Sitecore.DependencyInjection;
using Sitecore.XA.Foundation.RenderingVariants.Controllers;
using Sitecore.XA.Foundation.RenderingVariants.Repositories;

namespace PROJECTABC.Feature.Composites.SXA.Controllers
{
    public class DCardsController : VariantsController
    {
    private readonly IDCardsRepository DCardsRepository;

    public DCardsController(IDCardsRepository repository)
    {
        DCardsRepository = repository;
    }


    protected override object GetModel()
    {
        return DCardsRepository.GetModel();
    }

    }
}

Models\DCardsModel.cs

using Sitecore.XA.Foundation.Variants.Abstractions.Models;

namespace PROJECTABC.Feature.Composites.SXA.Models
{
    public class DCardsModel : VariantsRenderingModel
    {
        public string CustomProperty { get; set; }
    }
}

Repositories\DCardsRepository.cs

using PROJECTABC.Feature.Composites.SXA.Models;
using Sitecore.XA.Foundation.Mvc.Repositories.Base;
using Sitecore.XA.Foundation.RenderingVariants.Repositories;

namespace PROJECTABC.Feature.Composites.SXA.Repositories
{
    public class DCardsRepository : VariantsRepository, IDCardsRepository
    {
        public override IRenderingModelBase GetModel()
        {
            DCardsModel model = new DCardsModel();
            FillBaseProperties(model);
            model.CustomProperty = "This is just an example rendering";
            return model;
        }
    }
}

Repositories\IDCardsRepository.cs

using Sitecore.XA.Foundation.Mvc.Repositories.Base;

namespace PROJECTABC.Feature.Composites.SXA.Repositories
{
    public interface IDCardsRepository : IVariantsRepository
    {
    }
}

Views\DCards\DCards.cshtml

@using Sitecore.Extensions
@using Sitecore.XA.Foundation.MarkupDecorator.Extensions
@using Sitecore.XA.Foundation.SitecoreExtensions.Extensions
@using Sitecore.XA.Foundation.RenderingVariants.Extensions
@using Sitecore.XA.Foundation.RenderingVariants.Fields
@using Sitecore.XA.Foundation.Variants.Abstractions.Fields
@model IRAS.Feature.Composites.SXA.Models.DCardsModel

<div @Html.Sxa().Component("DCards", Model.Attributes)>
    <div class="component-content">

        <h1>@Model.CustomProperty</h1>
        @foreach (BaseVariantField variantField in Model.VariantFields)
            {
            @Html.RenderingVariants().RenderVariant(variantField, Model.Item, Model.RenderingWebEditingParams)
        }
    </div>
</div>

RegsiterDependencies.cs

using Microsoft.Extensions.DependencyInjection;
using PROJECTABC.Feature.Composites.SXA.Controllers;
using PROJECTABC.Feature.Composites.SXA.Repositories;
using Sitecore.DependencyInjection;

namespace PROJECTABC.Feature.Composites.SXA
{
    public class RegisterDependencies : IServicesConfigurator
    {
        public void Configure(IServiceCollection serviceCollection)
        {
            serviceCollection.AddTransient<IDCardsRepository, DCardsRepository>();
            serviceCollection.AddTransient<DCardsController>();
        }
    }
}

App_Config\Include\Feature\PROJECTABC.Feature.Composites.SXA.config

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <services>
      <configurator type="PROJECTABC.Feature.Composites.SXA.RegisterDependencies, IRAS.Feature.Composites.SXA" />
    </services>
  </sitecore>
</configuration>

In Sitecore

/sitecore/templates/Feature/DCards

enter image description here

/sitecore/system/Settings/Feature

enter image description here

/sitecore/layout/Renderings/Feature/DCards/DCard Content

I believe this is where the issue relies on.

enter image description here

enter image description here

/sitecore/content/Government/PROJECTABC/MyTaxPortal/Data/DCards/Card1

enter image description here

/sitecore/content/Government/PROJECTABC/MyTaxPortal/Presentation/Available Renderings/DCards

enter image description here

/sitecore/content/Government/PROJECTABC/MyTaxPortal/Presentation/Rendering Variants/DCards

enter image description here

Any thoughts?

Update 1:

I've tried changing the Controller field to: PROJECTABC.Feature.Composites.SXA.Controllers.DCardsController,PROJECTABC.Feature.Composites.SXA but i received the same issue

  • Your stack trace appears to be missing the inner exception. Also, I believe the registration code should be added back/enabled. Also, the constructor should pass in the repository rather than calling the service locator. – Michael West Feb 24 '18 at 7:39
1

I've resolved the issue, after publishing and viewing it from the site. It appears that it is because the view couldn't be located.

enter image description here

Revisiting the rendering name, I realized that SXA is using the item name of the rendering to locate the .cshtml, hence renaming it from 'DCards Content' to 'DCards' resolved the issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.