ASP.NET Core Rendering SDK
Contents Resolvers

ASP.NET Core Rendering SDK <br/>Contents Resolvers

Sometimes, it’s not enough to place all data into a single datasource. For example: you want to show all subitems of specific item and don’t want to add additional field into a parent and select all of them there. Contents resolvers is a new feature of Sitecore Headless that helps to resolve such cases since direct access to database is not possible from the Rendering Host app. All you need is to select needed one in your rendering and some extra data will be provided additionally. There are couple of resolvers with self-descriptive names available out of the box:

  • Context Item Children Resolver

  • Context Item Resolver

  • Datasource Item Children Resolver

  • Datasource Resolver

  • Folder Filter Resolver

  • Sitecore Forms Resolver

All of them may be found at /sitecore/system/Modules/Layout Service/Rendering Contents Resolvers folder. New items of custom resolvers have to be added here as well.

So, let’s create a rendering from our example that utilize one from default resolvers, let’s say, authors list for a corporate blog.

Create templates:

Author template:

Author template

Simplified to single field with author’s name.

Authors folder template:

Authors folder standard values

Just add standard values and select Author template in Insert options field to allow quick adding of a new items into a folder.

Create a Authors folder and add some Authors:

Authors folder

Create Authors rendering:

Rendering will be of type Json Rendering. And some values will be set:

  • Component Name: “AuthorsListing”

  • Datasource Template: select Authors Folder template

  • Datasource Location: select Settings folder (parent of Authors item)

  • Rendering Contents Resolver: select Datasource Item Children Resolver

After creating rendering it should be added to a page and authors folder should be set as datasource.

Create models at Rendering Host:

Model for each Author will look like this:

using Sitecore.LayoutService.Client.Response.Model.Fields;

namespace Rendering.Models.Renderings.Blog.Authors;

public class Author
{
    public TextField Name { get; set; }
}

Name for Authors listing will look like this:

using Sitecore.AspNet.RenderingEngine.Binding.Attributes;
using Sitecore.LayoutService.Client.Response.Model.Fields;

namespace Rendering.Models.Renderings.Blog.Authors;

public class AuthorsList
{
    [SitecoreComponentField(Name = "items")]
    public ContentListField<Author> Authors { get; set; }
}

Create Model-Bounded View:

Just list all names:

@model Rendering.Models.Renderings.Blog.Authors.AuthorsList

<ul>
    @foreach (var item in Model.Authors)
    {
        <li>@item.Fields?.Name?.Value</li>
    }
</ul>

View also should be bound in Rendering Engine setup:

builder.Services
    .AddSitecoreRenderingEngine(options =>
    {
        options
            .AddModelBoundView<Rendering.Models.Renderings.Blog.Authors.AuthorsList>("AuthorsListing", "AuthorsList")
            .AddDefaultPartialView("_ComponentNotFound");
    });

After finishing all steps above you can see following in a browser:

Authors result

You may notice that choosing child items contents resolver requires two models on Rendering Host side - one for actual item and one for collection. If you’re going to use child content resolvers you might be interested in creating generic model for a collection. Following code may be a good starting point:

using Sitecore.AspNet.RenderingEngine.Binding.Attributes;
using Sitecore.LayoutService.Client.Response.Model.Fields;

namespace Rendering.Models;

public class ChildrenList<T> where T : class
{
    [SitecoreComponentField(Name = "items")]
    public ContentListField<T> Items { get; set; }
}

You can use this model width specific types, so you’ll need to create models only for those templates.

Good new, in a good traditions of Sitecore, you can create your own content resolvers. We'll review this process in next post.