1

When I get the data from http://{domain.name}/sitecore/api/layout/render/jss?item=/&sc_apikey=[api-key]

Then response is like {sitecore: {context:{something}, route:{something}}

But I don't want to include the route object if, in my custom layout service extension code some condition is not met.

1
  • @adam I have the same scenario to customize the output of route object. The customization needs to be done without touching the code of components. Could you please suggest which pipeline or code used for it. Apr 1, 2020 at 10:15

1 Answer 1

3

The most straightforward approach will likely be to extend and customize the Sitecore.JavaScriptServices.ViewEngine.LayoutService.Serialization.LayoutTransformer, specifically, the Transform(RenderedItem rendered) method.

The default LayoutTransformer is declared via config in /App_Config/Sitecore/LayoutService/Sitecore.LayoutService.Jss.config.

You'll see the jss named configuration for Layout Service:

<config name="jss">
  <rendering type="Sitecore.LayoutService.Configuration.DefaultRenderingConfiguration, Sitecore.LayoutService">
    <placeholdersResolver type="Sitecore.LayoutService.Placeholders.DynamicPlaceholdersResolver, Sitecore.LayoutService" />
    <itemSerializer type="Sitecore.JavaScriptServices.ViewEngine.LayoutService.JssItemSerializer, Sitecore.JavaScriptServices.ViewEngine" resolve="true">
      <AlwaysIncludeEmptyFields>true</AlwaysIncludeEmptyFields>
    </itemSerializer>
    <renderingContentsResolver type="Sitecore.LayoutService.ItemRendering.ContentsResolvers.RenderingContentsResolver, Sitecore.LayoutService">
      <IncludeServerUrlInMediaUrls>true</IncludeServerUrlInMediaUrls>
    </renderingContentsResolver>
  </rendering>
  <serialization type="Sitecore.JavaScriptServices.ViewEngine.LayoutService.SerializationConfiguration, Sitecore.JavaScriptServices.ViewEngine">
    <transformer type="Sitecore.JavaScriptServices.ViewEngine.LayoutService.Serialization.LayoutTransformer, Sitecore.JavaScriptServices.ViewEngine" resolve="true" />
  </serialization>
</config>

And within that named configuration is the LayoutTransformer declaration (config/rendering/serialization/transformer).

Lastly, a custom layout transformer may look something like below. Note that if the custom condition is met, then the route object will not be in the final output.

public class CustomLayoutTransformer : LayoutTransformer    
{
    public CustomLayoutTransformer(IPlaceholderTransformer placeholderTransformer) : base(placeholderTransformer)
    {
    }

    public override dynamic Transform(RenderedItem rendered)
    {
        var baseResult = base.Transform(rendered);

        if (myCustomCondition)
        {
            dynamic newResult = new ExpandoObject();
            newResult.sitecore = new
            {
                baseResult.context
            };
            return newResult;
        }

        return baseResult;
    }
}
3
  • I tried this way also but the thing is that I am not able to get the route object, but I am able to get the context object. My requirement is with route object. Dec 18, 2018 at 9:16
  • Your question said you want to remove the route object, the CustomLayoutTransformer code snippet provided above will do that. Is there something else you need to accomplish?
    – Adam Weber
    Dec 18, 2018 at 18:53
  • Okay, I understood now @Thanks a lot. I just understood the code as present in different pipeline. Dec 21, 2018 at 14:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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