top of page
div.main-title-section-bg (1).png

Blog Detail

Home / About

Customizing Experiences: Enhancing Sitecore JSS with Content Resolver

Content Resolver in Sitecore JSS




Sitecore Headless provides six rendering contents resolvers

Path: /sitecore/system/Modules/Layout Service/Rendering Contents Resolvers.

Content Resolver types & Definitions

  • 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.



  • The class should inherit the Sitecore.LayoutService.ItemRendering.ContentsResolvers.RenderingContentsResolverand must override ResolveContents method of the base class.

  • Content resolver class file -- code


Namespace and Imports:

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using Sitecore;

using Sitecore.Data.Items;

using Sitecore.Diagnostics;

using Sitecore.LayoutService.Configuration;

using Sitecore.LayoutService.ItemRendering.ContentsResolvers;

using Sitecore.Mvc.Presentation;

using Newtonsoft.Json.Linq;

 

namespace content_resolver.Controller

{

CustomRenderingContentsResolver Class:

    public class CustomRenderingContentsResolver : RenderingContentsResolver

{

 

        

ResolveContents Method Override:

 

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<Item> items = GetItems(datasourceItem);

            List<Item> 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 layout service file - CustomRenderingContentsResolver


  • 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. 






  • Created New component in Banner section






  • l Postman url send then display the json file .





bottom of page