1

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
2

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 create 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:///sitecore/admin/showconfig.aspx:

<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>

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.