Contact Us

Sitecore’s Layout Service is a Headless endpoint that is responsible for providing data related to the requested page or rendering in the form of a JSON object.

Rendering Content Resolvers when used in combination with the Layout Service provides complex Sitecore Content of the rendering again in the form of a JSON Object.

Sitecore Headless provides six ‘Rendering Contents Resolvers’, by default, and they are found here: /sitecore/system/Modules/Layout Service/Rendering Contents Resolvers.

Image 1

  • Context Item Children Resolver: resolves/serializes the children of the context item
  • Context Item Resolver: resolves/serializes the context item
  • Datasource Item Children Resolver: resolves/serializes the children of the rendering’s datasource item
  • Datasource Resolver: resolves/serializes the rendering’s datasource item
  • Folder Filter Resolver: resolves items of Folder template type. The Template ID is specified in the Item Selector Query field of the resolver item in Sitecore.
  • Sitecore Forms Resolver: resolver specific to form items

 

These default resolvers address the majority of situations, but while working on a JSS site, there is a particular situation that custom resolvers may be required to address.

For example, obtaining the fields and details of a datasource item along with its children in a single JSON object is a frequently occurring case that is not supported by any of the resolvers described above.

Let us, therefore, walk through this simple process of developing a custom rendering content resolver, which entails:

  • Creating the class type where we will implement our own Sitecore content resolution mechanism.
  • Creating a Resolver Sitecore Item to allow users to select it from the list of available resolvers.

 

We will consider Datasource Item with Children Resolver as an example in the detailed process mentioned below.

Creating the class type where we will implement our own Sitecore content resolution mechanism

The class should inherit from the

Sitecore.LayoutService.ItemRendering.ContentsResolvers.RenderingContentsResolver and must override the ResolveContents method of the base class.

Code snippet of the class (please read the inline comments to know the intent of the code written):

using System.Collections.Generic;

using System.Linq;

using Newtonsoft.Json.Linq;

using Sitecore.Data.Items;

using Sitecore.Diagnostics;

using Sitecore.LayoutService.Configuration;

using Sitecore.LayoutService.ItemRendering.ContentsResolvers;

using Sitecore.Mvc.Presentation;

namespace Sitecore.Customizations.RenderingContentResolvers

{

public class DatasourceWithChildrenItemResolver : RenderingContentsResolver

{

public override object ResolveContents(Rendering rendering, IRenderingConfiguration renderingConfig)

{

//check if the parameters are not null

Assert.ArgumentNotNull(rendering, nameof(rendering));

Assert.ArgumentNotNull(renderingConfig, nameof(renderingConfig));

//get the datasource item

Item datasourceItem = this.GetContextItem(rendering, renderingConfig);

//return null object if the datasourceItem is null

if (datasourceItem == null)

return null;

//initialize the JSON object to be returned with the datasourceItem details

JObject jobject = ProcessItem(datasourceItem, rendering, renderingConfig);

//get the children of the datasourceItem

IEnumerable items = GetItems(datasourceItem);

List itemList = items != null ? items.ToList() : null;

//return the JSON object if children do not exist

if (itemList == null || itemList.Count == 0)

return jobject;

//add children item details to the JSON object and return the object

jobject["items"] = ProcessItems(itemList, rendering, renderingConfig);

return jobject;

}

}

}

 

Creating a Resolver Sitecore Item to allow users to select it from the list of available resolvers

It is recommended to create the custom rendering resolvers in a separate folder under /sitecore/system/Modules/Layout Service/Rendering Contents Resolvers.

  • Create a new folder of /sitecore/templates/System/Layout/Layout Service/Rendering Contents Resolvers Folder type inside Rendering Contents Resolvers using the insert options.
  • Create the custom content resolver, again, using the insert options.

Image 2

As an example, here we have created Datasource Item with Children Resolver. Now let’s look at the fields of the resolver items (refer to the default content resolver, they are fairly intuitive with the short description provided):

1. Type: Put the namespace of the custom resolver class and its assembly like so: Customizations.RenderingContentResolvers.DatasourceWithChildrenItemResolver, Sitecore.Customizations

2. Include Server URL in Media URLs: checked by default.

3. Use Context Item: this field is unchecked, by default, which means that the resolver will use the datasource item (of the rendering) rather than the Context Item. When it is selected, the JSON object will include route-level data. For our example, we will keep it unchecked.

4. Item Query Selector: This field allows the user to add queries that can manipulate how the content is being processed. For the default folder content resolver, this field contains the ID of the “Folder” item and only those items will be included while processing the Sitecore Content.

It will be available in the list of resolvers in the Rendering Contents Resolver field of the rendering. With this custom resolver selected, the JSON output will include data of the datasource item along with its children.

Image 3

In Conclusion

This blog has attempted to break down the process involved in implementing Sitecore’s Layout Service along with the Custom Rendering Contents Resolver.

But you may still have queries about its implementation within the context of your specific business requirements. Reach out to us for both expert guidance and implementation of all things Sitecore.

Need Help?