Sitecore XMCloud Extending Layoutservice response using RenderingContentResolver

 We have different way to show the customize the layout service data

  • Rendering Content Resolver (For rendering level customization)
  • Layout service Context data (Applicable for context level in the Layoutservice response mostly used to share the static data to all components/pages etc)
  • Rendering's Integrated GraphQL
Steps to Customizing the LayoutService data using Rendering Content Resolver.
  • Clone the starter kit from github (https://github.com/sitecorelabs/xmcloud-foundation-head-staging) or create own visual studio solution by adding the reference to the following nuget packages.


  • Once reference added , create a Contentresolver class necessary to provide data to the rendering.
  • On build if receive any error related to the nuget package version like below
    •  

using Sitecore.LayoutService.Configuration; using Sitecore.LayoutService.ItemRendering.ContentsResolvers; using System; using System.Collections.Generic; using System.Linq; using System.Web; using Sitecore.Configuration; using System.Collections.Specialized; using Sitecore.Mvc.Presentation; namespace XmCloudSXAStarter.ContentResolver { public class UserDataContentsResolver : IRenderingContentsResolver { public bool IncludeServerUrlInMediaUrls { get; set; } public bool UseContextItem { get; set; } public string ItemSelectorQuery { get; set; } public NameValueCollection Parameters { get; set; } public object ResolveContents(Rendering rendering, IRenderingConfiguration renderingConfig) { //if you want to access the datasource item var datasource = !string.IsNullOrEmpty(rendering.DataSource) ? rendering.RenderingItem?.Database.GetItem(rendering.DataSource) : null; return new { //this returns the custom anonymous object with the results from datasource //also from customization( any custom sitecore api call or hardcorded , //any 3rd party rest api call to get data, etc) Name = $"Mr.{datasource.Fields["Title"].Value}", Description = datasource.Fields["Description"].Value //Blogs="" Data from sitecore api or from 3rd party rest etc }; } } }
  • Once code ready, publish this code to the running docker container, if we trying to access the content from local docker sitecore instance, or if trying to get the data from XMcloud directly push the code to github same repository from this XMCloud instance created, which will trigger the deployment to XMcloud with these updates.


  • Lets execute our GraphQL query.


  • Accessing the Layoutservice response in Nextjs application






Happy Programming 😊

Comments

Popular posts from this blog

Custom Item Url and resolving the item in Sitecore - Buckets

Exploring Sitecore XM Cloud Forms: A Comprehensive Overview

Sitecore Custom Rule (Action and Condition)