Create Custom Sitecore SXA Renderings and Components

Sitecore Hints & Tips

In this blog article, we take a look at the creation of custom Sitecore SXA renderingds and components. The blog post example has been created on following versions:

  • Using Sitecore 8.2 (rev 170407) - Update 3
  • Sitecore Experience Accelerator 1.3 rev. 170519 for 8.2.zip

Below we will see the steps to create SXA compatible custom rendering and SXA compatibility should have the following features:

  • Available in Toolbox
  • Would Support Drag and Drop
  • Will Help Content Authors (with help text and areas where they can insert)
  • Multiple Variants
  • SXA Out-of-the-Box (OOTB) Styling

Here, we're taking an example of a custom carousel where we have the following requirements and global settings:

  • Can have a background colour

Each carousel slide can have:

  • Title
  • Description
  • Image
  • Link
Sitecore SXA Carousel

Creating the Carousel

Following steps are required to create the above carousel.

Step 1 - Creating Sitecore Templates for Required Custom Fields

SXA Carousel

SXA is created from Helix principles, we've chosen "Media" as a feature to create the carousel in. The media folder will have 2 sub folders, Datasource and Rendering Parameters.

Datasource will have the Carousel Folder where all carousels will be created; the Carousel Template will have the global carousel properties like background colour and Carousel Slide with each slide properties.

Rendering Parameters folder will have Carousel rendering parameters and can inherit from OOTB templates like:

SXA Carousel

Step 2 - Create a Custom Carousel Controller Rendering

SXA Carousel

And we need to add this rendering to the list of available renderings to make it appear in the SXA toolbox:

SXA Carousel

Now jumping to the code.


Step 3 - We Need to add a Controller "CustomCurouselController" to Feature Media

SXA Carousel

As we can see in the above screenshot the controller needs to be inherited from "StandardContoller", which can be found under namespace "Sitecore.XA.Foundation.Mvc.Controllers".

You may have noted that we did not add any "ActionResult" to the controller class and nor did we have it in the Sitecore rendering. This is because the Standard controller will take care of your routing from the controller to view with default action result "Index". We can also add and override "Index" Actionresult here in the controller if required.


Step 4 - We need a Carousel Model to Create / Fetch Custom Carousel Properties

SXA Carousel

Our custom model should inherit from "VariantRenderingModel" which will hold some base SXA properties like: Datasource item, css class, heading tag, IsEdit etc.


Step 5 - The Repository will be as Follows

SXA Carousel

Should inherit from "VariantsRepository", which will make our custom carousel rendering extendible through variants.

"FillBaseProperties" is an OOB SXA method found under namespace "Sitecore.XA.Foundation.RenderingVariants.Repositories" which will fill the base SXA properties in our model.


Step 6 - Adding a View to the Media Project for Custom Carousel

SXA Carousel

Note: Every SXA component should be wrapped inside a div "component" and "component-content". Also, "Model.Attribute" will be used to add styles from the rendering parameters. (Refer any existing SXA view). That should be it!

We can see the new custom carousel in the toolbox and drag and drop it. Add a new partial view to quickly test it:

SXA Carousel

We can see the rendering in the toolbox:

SXA Carousel

— Chaitanya Marwah - August 2017

Want more?

Back to the Blog

Switch have been awarded a Sitecore Experience Award for Best in Connecting the Experience.
Read more
Sitecore Experience Award Winner 2022

Switch acknowledges the Traditional Custodians of the lands where we live, learn, and work and we pay our respects to their Elders past and present.

Sydney
Level 4
109 Pitt Street
Sydney NSW 2000
Phone +61 02 8188 5959
Melbourne
Level 22
120 Spencer St
Melbourne VIC 3000
Phone +61 03 8566 8819
New Zealand
11-19 Customs Street West
Auckland 1010

Phone +64 9 887 0338
Singapore
8 Cross Street
Singapore, 048424

Phone +65 3106 4217
ABN 15 120 572 750   ·   © 2023 Switch

Subscribe Now

Sign up to our newsletter to gain access to this page.

No thanks