0

I want to have TemplateName & TemplateID of the datasource item for a JSON rendering in the response of the LayoutService which doesn't comes by default. So I have created a custom patch.

Following are the code changes I have made :

CustomRenderingContentsResolver.cs :

protected override JArray ProcessItems(
  IEnumerable<Item> items,
  Sitecore.Mvc.Presentation.Rendering rendering,
  IRenderingConfiguration renderingConfig)
    {
        JArray jarray = new JArray();
        foreach (Item obj in items)
        {
            JObject jobject1 = this.ProcessItem(obj, rendering, renderingConfig);
            JObject jobject2 = new JObject()
            {
                ["id"] = (JToken)obj.ID.Guid.ToString(),
                ["templateId"] = (JToken)obj.TemplateID.ToString(),
                ["templateName"] = (JToken)obj.TemplateName.ToString(),
                ["url"] = (JToken)LinkManager.GetItemUrl(obj, ItemUrlHelper.GetLayoutServiceUrlOptions()),
                ["name"] = (JToken)obj.Name,
                ["displayName"] = (JToken)obj.DisplayName,
                ["fields"] = (JToken)jobject1
            };
            jarray.Add((JToken)jobject2);
        }
        return jarray;
    }

Config File For Patching :

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
    <layoutService>
        <configurations>
            <config name="jss-rendering">
                <rendering>
                    <renderingContentsResolver type="UMC_SC.Foundation.Sites.Platform.LayoutServiceResolvers.CustomRenderingContentsResolver, UMC_SC.Foundation.Sites.Platform" patch:instead="renderingContentsResolver[@type='Sitecore.LayoutService.ItemRendering.ContentsResolvers.RenderingContentsResolver, Sitecore.LayoutService']" resolve="true">
                        <IncludeServerUrlInMediaUrls>false</IncludeServerUrlInMediaUrls>
                    </renderingContentsResolver>
                </rendering>
            </config>
        </configurations>
    </layoutService>
</sitecore>

My Patch also got applied properly in showconfig :

enter image description here

Next I have configured Datasource Resolver in the Rendering Contents Resolver for the rendering :

enter image description here

Although still, I am not getting templateID & templateName fields in the LayoutService reponse :

enter image description here

Am I missing any other steps?

Can anyone pls help me out?

Thanks.

2
  • If I understood properly, you want the template name of the datasource item? Dec 12, 2021 at 15:54
  • Actually I want templateID but even templateName of the datasource item will also work.
    – Anvay
    Dec 12, 2021 at 18:26

3 Answers 3

0

First potential issue:

It looks like you're creating an entirely new config called "jss-rendering". Was this intentional, and did you set up your site to use this config?

If using JSS+SXA your Site settings should have the "Layout service configuration" field set to your custom jss-rendering rather than the default jss.

If not using SXA, the add the attribute to your app node:

<javaScriptServices>
<apps>
<app name="my-app-name" layoutServiceConfiguration="jss-rendering"/>
</apps>
</javaScriptServices>

Second potential issue:

You said

Next I have configured Datasource Resolver in the Rendering Contents Resolver for the rendering

I believe this will override the resolver you set in the config. My understanding is that the resolver you set in the config is the default one, if you leave the resolver in the rendering as blank.

If you want to have a custom resolver that is only used for certain renderings, you will need to create an item under /sitecore/system/Modules/Layout Service/Rendering Contents Resolvers and set the type to your custom resolver. Then it should show up in the droptree for you to select instead of "Datasource Resolver"

0

Wrong method. You need to override protected virtual JObject ProcessItem(Item item, Rendering rendering, IRenderingConfiguration renderingConfig) as well.

This is the code where you are: enter image description here

0

Each Resolver is bind with Assembly reference using the Type field as Below . enter image description here

Rather than changing the default we should go for Custom Resolver which is explained Here https://www.romeocadaoas.com/blog/programming/sitecore-jss-rendering-content-resolver/ But the key is that Resolver will be using the Type

1
  • Please provide more details to improve the quality of your answer. yesterday

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.