Sitecore JSS Limit depth of items retrieved in linked fields

559 Views Asked by At

I have an App Route A for a page and is loading all the fields including a multilist that links to Item App Route B that can have in the multilist a link to Item A, creating a infinite loop. Is there a way to set the depth to only one level?

AppRoute A:
  fields:
    multifield:
      AppRoute B

AppRoute B:
  fields:
    multifield:
      AppRoute A
1

There are 1 best solutions below

0
Jaime Escobar On BEST ANSWER

There are two options, you can create a patch for the configuration file to update the layoutService serializationMaxDepth, but this will affect all the Items:

<configuration>
  <sitecore>
    <settings>
    <layoutService>
      <serializationMaxDepth>2</serializationMaxDepth>
    </layoutService>
  </sitecore>
</configuration>

Or you can craete your custom field serialization resolver.

This class will add the information to the field:

public class CustomFieldSerializer : BaseFieldSerializer
{
    public CustomFieldSerializer (IFieldRenderer fieldRenderer)
      : base(fieldRenderer)
    {
    }


    protected override void WriteValue(Field field, JsonTextWriter writer)
    {
      writer.WriteStartObject();
      writer.WritePropertyName(field.Name);
      writer.WriteValue("Your custom field value here.");
      writer.WriteEndObject();
    }
  
}

This class will be the field resolver:

    public class GetCustomFieldSerializer : BaseGetFieldSerializer
  {

    public GetCustomFieldSerializer(IFieldRenderer fieldRenderer)
        : base(fieldRenderer)
    {
    }


    protected override void SetResult(GetFieldSerializerPipelineArgs args)
    {
      Assert.ArgumentNotNull((object)args, nameof(args));
      args.Result = new CustomFieldSerializer(this.FieldRenderer);
    }
  }

And this is the configuration patch to set the resolver class to your field. Make sure to place your config(patch:before) before the field default serialization configuration, in this case the multililist is GetMultilistFieldSerializer you can check this in https://<your-sitecore-domain>/sitecore/admin/showconfig.aspx:

<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <pipelines>
      <group groupName="layoutService">
        <pipelines>
          <getFieldSerializer>
            <processor patch:before="processor[@type='Sitecore.LayoutService.Serialization.Pipelines.GetFieldSerializer.GetMultilistFieldSerializer, Sitecore.LayoutService']" type="Foundation.FieldSerializer.GetCustomFieldSerializer, Foundation.LayoutService" resolve="true">
              <FieldTypes hint="list">
                <fieldType id="1">multilist</fieldType>
              </FieldTypes>
            </processor>
          </getFieldSerializer>
        </pipelines>
      </group>
    </pipelines>
  </sitecore>
</configuration>