Insights

Retrieving the Datasource Link Instead of ItemID in Sitecore Layout Service

Learn how to retrieve the datasource link instead of ItemID in layout service.

Current Situation

When we look at the Json-formatted Layout Service in Sitecore, we get to see the components are serialized with their datasource as ItemIDs, as seen in the image below. But while working on a project, I encountered a situation wherein instead of the ItemID, I had to retrieve the link of the particular datasource of the component. One thing to keep in mind is to alter the datasource value only for this particular component in the Layout Json without affecting the datasource for other components.

Retrieving Datasource Link instead of ItemID in Layout Service

The solution is to override the initialize pipeline processor in Sitecore.LayoutService.Presentation.Pipelines.RenderJsonRendering and patch it into the Layout Service pipeline configuration. (I referred to Andy Parry’s blog, Serializing rendering parameters in the layout service, for implementing this requirement.)

Patching the Layout Service Pipeline Processor

Overriding the Initialize method of RenderJsonRendering allows us to extend the datasource property in the RenderedJsonRendering object returned by the pipeline processor with serialized values instead of actual values.

using System.Collections.Generic;
using Sitecore.Data;
using Sitecore.Data.Items;
using Sitecore.LayoutService.Configuration;
using Sitecore.LayoutService.ItemRendering;
using Sitecore.LayoutService.Presentation.Pipelines.RenderJsonRendering;
using Sitecore.Mvc.Presentation;
using System.Linq;
using Sitecore.Links;

namespace JSSDemo.Project.Foundation.Extensions.Pipelines
{
    public class CustomInitialize : Initialize
    {
        IRenderingConfiguration _renderingConfiguration;

        public CustomInitialize(IConfiguration configuration) : base(configuration) { }

        protected override RenderedJsonRendering CreateResultInstance(RenderJsonRenderingArgs args)
        {
            _renderingConfiguration = args.RenderingConfiguration;

            //Note: the constructor below is different for Sitecore 9.x and 10. The below will only work in Headless Services for Sitecore 10.
            return new RenderedJsonRendering()
            {
                ComponentName = args.Rendering.RenderingItem.Name,               
                DataSource = SerializeDatasource(args.Rendering),
                RenderingParams = args.Rendering.Parameters.ToDictionary(pair => pair.Key, pair => pair.Value),
            Uid = args.Rendering.UniqueId
            };
        }

    protected virtual string SerializeDatasource(Rendering rendering)
    {
            if (rendering.RenderingItem.Name.Equals("NewsImageCard"))
            {

                Item datasourceItem = rendering.Item;
                if (datasourceItem == null) return null;
                var options = LinkManager.GetDefaultUrlBuilderOptions();
                options.AlwaysIncludeServerUrl = false;
                string link = LinkManager.GetItemUrl(datasourceItem, options);
                return link;
            }
            else { return rendering.DataSource; }

    }
}
}

This is then patched into the Layout Service renderJsonRendering pipeline:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<pipelines>
<group groupname="layoutService">
<!-- NOTE: This patch will have a slightly different signature in Headless Services for Sitecore 9.x vs 10. The below works for Headless Services in Sitecore 10 -->
<pipelines>
<renderjsonrendering>
<processor type="JSSDemo.Foundation.LayoutService.PipelineProcessors.CustomInitialize, JSSDemo.Foundation.LayoutService" resolve="true" patch:instead="*[@type='Sitecore.LayoutService.Presentation.Pipelines.RenderJsonRendering.Initialize, Sitecore.LayoutService']">
</processor>
</renderjsonrendering>
</pipelines>
</group>
</pipelines>
</sitecore>
</configuration>

Once all these changes are implemented and the project is published, we will be able to see the datasource of that particular component renders as a link

Retrieving Datasource Link instead of ItemID in Layout Service

Thanks for reading through, and I hope you found my little blog informative.

Happy Sitecoring!

👋 Hey Sitecore Enthusiasts!

Sign up to our bi-weekly newsletter for a bite-sized curation of valuable insight from the Sitecore community.

What’s in it for you?

  • Stay up-to-date with the latest Sitecore news
  • New to Sitecore? Learn tips and tricks to help you navigate this powerful tool
  • Sitecore pro? Expand your skill set and discover troubleshooting tips
  • Browse open careers and opportunities
  • Get a chance to be featured in upcoming editions
  • Learn our secret handshake
  • And more!
Sitecore Snack a newsletter by Fishtank Consulting
 

Meet Anju Thomas

Sitecore Web Developer

🎞🥘🧩

Anju is a Sitecore Developer with a Bachelor's Degree in Computer Science and over 4 years of experience in Sitecore development. She loves solving Sudoku puzzles, watching movies, eating delicious food and exploring the world.

Connect with Anju