Building Components in Sitecore 10 with the ASP.NET Rendering SDK

Last Thursday I presented at the London Sitecore User Group Online. This was the first online UK Sitecore user group this  year, which was a response to the Covid-19 Pandemic.

I Demo’ed how to Build ASP.Net Core Components in Sitecore 10 with the ASP.Net Rendering SDK.  The Demo uses a modified version of the Getting Started template to do this. An overview of the ASP.Net Rendering SDK is also provided and some useful Tips too.

Video Of Presentation

You can find a pre-recording of my presentation on my YouTube Channel here:

Steps To Build a Component in Sitecore 10

Bellow I’m going to detail the steps involved with Building a component in Sitecore 10 using the ASP.Net Rendering SDK. These are covered in the video but I thought it would be useful to write them down too and provide some further information where applicable.

Development Environment Setup

The first step is to get a Sitecore 10 Development environment setup. For my demo video above I used the getting started template. The Demo Video and this Overview starts from the point of having this installed. You could use docker and install everything yourself or install Sitecore 10 directly on your local development environment instead. However you would need to create your own Visual Studio project for the Rendering Host so this is more complex if you are just looking to experiment with this like I was.

Fellow MVP Robbert Hock has a really helpful video on getting the getting started template all setup and working so I’m not going to go into this further here: https://www.kayee.nl/2020/09/25/getting-started-with-sitecore-headless-development-asp-net-core-docker-containers/

note: to use the new ASP.Net Rendering Host SDK you will need to have access to the Layout Service within your Sitecore Licence, I believe currently this means you need JSS included in your licence.

I modified the Getting Started Template a little to include Bootstrap 4, some custom CSS and some new placeholders and components to make things a bit more interesting, but other than that my demo follows this fairly closely.

mandalorian

Building a Card Component

As covered in the demo we are going to build a Card Component (part of bootstrap) and follow a similar approach to the official documentation example for building a component. Assuming you’ve installed the getting started Template with the defaults the Sitecore CM instance will be visible here: https://cm.myproject.localhost/sitecore. If you wish you can add bootstrap 4 from here to the layout file in the Getting Started Template so that what you see will look more similar to the screen shot above. After completing all the steps below you could cerement your knowledge by adding an hero and some other components too (like I have above).

UPDATE (16/12/2020): 
I got asked if my version is available when I presented this at the Pittsburgh SUG Recently so I've uploaded it to Github here for those that want to replicate the look and feel of my demo or look at the code I used.

This version also uses Rich text for the Text field and and includes an image field: https://github.com/fluxdigital/Mandalorian.Sitecore.NetCore
mandalorian-netcore-demo

Create the Data Template

Create an data template called Card in the following location: /sitecore/templates/Project/MyProject. I called the data field section ‘Card Data’ but you can call it what you want.

Add the following 3 fields and set an icon:

  • Title  – Single Line Text Field
  • Text – Multiline Text Field
  • Link – General Link Field

template

Create the Json Rendering

Create a new rendering using the ‘Json Rendering’ template called Card in the following location: /sitecore/layout/Renderings/Project/MyProject/Card. Leave the Component name as ‘Card’.

rendering

Set the Datasource Location to: ./ ,set the template to the Card template you just created and set an icon:

rendering2

Create a New Placeholder

Create an new placeholder called myproject-container in the following location: /sitecore/layout/Placeholder Settings/Project/MyProject. Add the Card to the allowed controls.

placeholder

Add to Layout Service placeholders

The layout used by the Home item is as follow: sitecore/content/MyProject/Home. We need to Tell the layout service to return the placeholder.

Open the layout here: /sitecore/layout/Layouts/Project/MyProject/Main and add the myproject-container placeholder to the layout service placeholders field.

layoutservice

There is documentation on this here but it’s not 100% clear that this also applies to renderings: https://doc.sitecore.com/developers/100/developer-tools/en/layout-and-site-requirements-for-asp-net-core-rendering.html.

Add the Card Item in Sitecore

Add Card Item under the Home Item and populate the fields with some data.

demo-card

Add the Card to the Page

Open home item in Experience Editor and add the card to the placeholder and save the page.

exp

You will not see the card appear and will probably show an error message in place of the card at this point as we have yet to build the Component in Visual Studio and inform the Rendering Engine about our Component.

unknown

Publish the Site

Within the CM Smart Publish the Site.

publish

View Layout Service Debugging

Within the console you can Show the layout service response by typing the following command:

docker-compose logs -f rendering

docker

If you scroll down the logs you should see an empty response in the layout service for the myproject-container as we need to do some work in Visual Studio now to get this working.

Create the Model

Within Visual Studio Create a new Model called CardModel at the following location: rendering\Models with the following contents:

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Threading.Tasks;
 using Sitecore.LayoutService.Client.Response.Model.Fields;
 namespace MyProject.Models
 {
   public class CardModel
   {
    public TextField Title { get; set; }
    public TextField Text { get; set; }
    public HyperLinkField Link { get; set; }
   }
 }

Note we are not using GlassMapper here we are using the new LayoutService Field Model. We use the TextField and HyperLinkFields for our Model.

Create the View

Create a new view called Card.cshtml within the following folder: rendering\Views\Shared\Components\SitecoreComponent\.

Add the following mark-up to include the 3 fields we created on our template earlier:

 @model MyProject.Models.CardModel
 <div class="card">
   <div class="card-body">
    <h2 class="card-title" asp-for="Title"></h2>
    <p class="card-text" asp-for="Text"></p>
    <sc-link asp-for="Link" class="btn btn-primary" editable="true"></sc-link>
  </div>
 </div>

Add Rendering Engine Binding

Within the Statup.cs file add the following line in bold to register the Card Model and Card View with the Rendering Engine.

// Register the Sitecore Rendering Engine services.
 services.AddSitecoreRenderingEngine(options =>
 {
 //Register your components here
 options
 .AddModelBoundView<CardModel>("Card")
 .AddDefaultPartialView("_ComponentNotFound");
 })

Add Placeholder to the Index Page

Update the index.cshtml layout file (rendering\Views\Default\Index.cshtml) to match the following. The main change here is to add the myproject-container placeholder:

 @model PageModel 
 @{
    ViewData["Title"] = Model.Title?.Value ?? string.Empty;
 }
 <main role="main">
    <sc-placeholder name="myproject-main"></sc-placeholder>
    <div class="container">
     <div class="row">
      <div class="col-md-8">
         <sc-placeholder name="myproject-container"></sc-placeholder>
      </div>
     </div>
   </div>
 </main>
 <nav class="navbar fixed-bottom navbar-expand-sm navbar-dark bg-dark" style="color: #fff;">
 &copy; @Model.CopyrightYear
 </nav>

Build the Project

Save and build the project to deploy the changes to the Rendering Host. As the Rendering Host is configured to use dotnet watch it should build and deploy the changes to the rendering host on save, so an build shouldn’t be needed really.

vs

View the page

Refresh the published page (by default: https://www.myproject.localhost/) to view the Updated home showing the new card component.

final-card

Your card may look a little different to this but it should be similar. Well done you’ve just built your first component in Sitecore 10 :-).

Tips

There are a few tips I picked up during my experimenting with this.

  • Check the Docker Console & Logs for errors if you are having problems:

    docker-compose logs -f rendering

  • If you have issues with Rendering Host or CM not updating then restart them both like so (docker can assign a new IP if you don’t restart both):

    docker-compose restart rendering cm

  • Read Up On Tag Helpers – https://doc.sitecore.com/developers/100/developer-tools/en/tag-helpers.html

Hopefully the video of my presentation and overview are useful for those who are considering using this new functionality in Sitecore 10 or are interested in knowing more about how this works.

Lastly thanks to Nick Wesselman for his help with some issues I had along the way and some diagrams for presentation.

Sitecore Symposium 2020 Highlights

sym-2020-banner

On Tuesday and Wednesday last week it was Sitecore Symposium 2020. This year it was all on-line so was a bit different and that was great in terms of catching up on all the content but not so great from a networking and social perspective.

I normally write a fairly detailed over-view of what I learned in the sessions over the 2-3 days; partly to share what I learned with others but also so I don’t forget what I learned either by the time I get back to the UK.
However this time round as most of the sessions are available on https://sym.sitecore.com/ you can watch a lot of it yourself if you missed it. I had an ‘All Access Pass’ so you will need to upgrade to view some of the content I saw.

So instead I’m going to summarise the 6 key themes over the 2 days instead with some links to key sessions I watched. I’ve kept the slides below to a minimum, but please check out my Twitter feed for more screen shots.

1. Pandemic Impact & Agility

The Pandemic has changed how customers expect to interact with brands and companies have had to respond very quickly to rapidly changing customer needs. In the opening Keynote and theme throughout Symposium was that a “Moment to Moment” Mindset is what is required to meet customers needs in these moments and how Sitecore’s innovations can help with achieving this.

Screenshot 2020-10-30 202449

Brands need to move a lot faster in the current climate and solve their internal operational challenges responds quickly to customers.

Screenshot 2020-10-30 203657

2. Auto Personalisation

The new Sitecore CEO Steve Tzikakis announced that all Sitecore 10 customers would receive access to Auto Personalisation Standard at no additional cost. This is clearly a key innovation for Sitecore going forwards.

Screenshot 2020-10-30 204102

There were some demos of this in action across the various sessions and It looks pretty cool. It could certainly take away a lot of the heavy lifting needed for personalisation and allow content editors to focus on other tasks configured and trained correctly.

Screenshot 2020-10-28 104748

We saw an example of how this works, how you can identify (and fix) customer ‘clusters’ which have gaps in personalisation and an example of the dashboard you would see showing performance and other metrics.

Screenshot 2020-10-28 104134

Screenshot 2020-10-28 104409

3. Content as a Service (CaaS)

Steve Tzikakis also announced that CaaS would be available in both managed cloud and Public Cloud. CaaS will deliver Sitecore content to any digital channel required, providing direct access to content in a decoupled way, allowing for faster development and delivery.

Screenshot 2020-10-27 232144

Screenshot 2020-10-27 213844

In a later CaaS preview session by Alistair Deneys & Andy Cohen we were given a lot more insight into how this will work and also told that it will not require Content Hub to work either as CaaS can be used directly against Sitecore too (via the JSS Layout Service). It’s great to see a full Headless offering from Sitecore using GraphQL to access the data and support for Next.Js too.

Screenshot 2020-10-27 214334

4. Content Hub

There were a lot of mention of Content Hub at Symposium this year, Jake the compère and comedian (who was pretty funny still despite having no live audience) joked that we should drink each time we here it.  I guess it makes sense we’d hear it a lot as Content Hub is a the Centre of Sitecore’s SaaS approach.
For those that don’t know Sitecore Aquired Content Hub (then Stylelabs) in 2018 and it both an DAM, PCM and CMs with lots of impressive functionality for managing and organising content for all channels in a single location.

Screenshot 2020-10-27 212222

Screenshot 2020-10-27 212423

It was really interesting is to see how Sitecore are using Content Hub for Sitecore.com and the challenges they faced.

Screenshot 2020-10-27 212858

One of the other sessions I watched on Content Hub was from Fellow Sitecore MVP Akshay Sura from Konabos and Sumith Damodaran from Sitecore. This talk was regarding how the new Content Hub Connector allows Sitecore Customers to integrate content from Sitecore XP into Content Hub where they can organise, collaborate and personalise content before distribution.

Screenshot 2020-11-01 191918

It was interesting to see how Sitecore fields are mapped on the Sitecore Template to the fields in Content Hub in the CMP connector and then how this displays in Content Hub.

Screenshot 2020-11-01 191653

CMP is available for Sitecore 9.2 and 9.3 currently and it may be merged with the DAM Connector in an upcoming release.

Screenshot 2020-11-01 192218

5. Containers are the Future

Container support was released as part of the new DevEx with Sitecore 10 and it will soon become the defacto way to develop locally for Sitecore. Whilst Containers do bring some complexity they also provide a lot of benefits such as portability, consistancy, being able to quickly destroy and re-create Sitecore instances and having multiple client instances or versions on a single machine.

Screenshot 2020-10-28 114955

However another one of the drivers for using containers is that they can be used in Production and you will soon be able to deploy to AWS, Google Cloud, your own Cluster or AKS! This means you will no be tied to Azure App Services.

As I understand it App Service Support will be marked as obsolete in Sitecore 10.1 and removed in Sitecore 10.2. This isn’t that Sitecore won’t work in App Services anymore but more that ARM templates will not be provided anymore and Support for Sitecore in App Services may be limited.

The session by Bart Plasmeijer and Rob Earlam on how to create an AKS Cluster, Windows Node Pool & deploy an Sitecore instance into it was really insightful. They managed to do all this within 20 minutes.

Screenshot 2020-10-28 115215

It was also really interesting to see some of the tooling in action like
@k8slens and K9s showing how they work and the features they provide.

Screenshot 2020-10-28 115726

Screenshot 2020-10-28 121157

There is another on-demand session on this too which was really informative around what the benefits are for Kubernetes for various roles in an organisation.

Screenshot 2020-10-30 223113

6. New Sitecore DevEx

A significant part of the Sitecore 10 release was the new Sitecore Development Experience (DevEx).
As well as containers for development one of the other new features is the availability of an new serialization option, which comes as standard with Sitecore. There are two flavours of this: the Sitecore CLI (using JSON) Or the GUI (Sitecore for VS). The CLI is free where as Sitecore for VS requires an (TDS) licence.

Screenshot 2020-10-27 204024

I saw an demo of this and it looked really interesting. I’ve tried it out myself too and it works really well. I’m interested to see if there are any features missing that unicorn or TDS have. It does look like Sitecore have most things covered though with the rules support.

Screenshot 2020-10-27 205944

Another part of the new DexEx is the new ASP.Net Core Rendering SDK which I saw an session on. This is a new way of building Sitecore sites in Sitecore 10.x, allowing you to use Layout Service to return content from Sitecore to display using .Net Core Components using Tag Helpers.

Screenshot 2020-10-28 200803

This approach has full support for Sitecore features such as editing in Experience Editor and also Analytics and Testing.

Screenshot 2020-11-01 185152

Nick also explained the .Net Core Request Pipeline works with the SDK, this is shown in the diagram below. It looks a little complicated but makes sense once you break it down.

Screenshot 2020-10-28 192411

There is a lot of new stuff to learn here but it’s great to see .Net Core support and another option available for Sitecore development. I’m learning more about this currently and plan to share what I learn in the near future.

It’s a wrap

The closing keynote with Leslie Odom Jr was entertaining and there were a lot of other on-demand sessions I watched too. It was really cool to hear that the next Symposium is going to be in Vegas next year :-), Hopefully I’ll see some of you there.

Screenshot 2020-10-28 215116

Screenshot 2020-10-28 215922

Thanks to Sitecore and the speakers for putting on another great event and for making it so accessible. Lets hope the Sitecore Community can meet up in Person in 2021!.