3

I need to retrieve Sitecore item fields and its properties with more than few levels and while running the below query

https://sc92instance/sitecore/api/ssc/aggregate/content/Items('76516a9f-203d-4006-b46e-a12fc87d8bc0')?$expand=Children($expand=Children($expand=Children,FieldValues), FieldValues), FieldValues

i get error. How can we increase MaxDepth of OData API in sitecore?

    {
    "error": {
        "code": "",
        "message": "The query specified in the URI is not valid. The request includes a $expand path which is too deep. The maximum depth allowed is 2. To increase the limit, set the 'MaxExpansionDepth' property on EnableQueryAttribute or ODataValidationSettings, or set the 'MaxDepth' property in ExpandAttribute."
    }
}
1
  • What is your Sitecore version? can you try - https://sc92instance/sitecore/api/ssc/aggregate/content/Items('76516a9f-203d-4006-b46e-a12fc87d8bc0')/Children?$expand=Fields. It looks your url string with query is not correct Commented Apr 28, 2020 at 9:17

1 Answer 1

3

MaxDepth == 2 is a default value of OData validator. You can override default values by registering your custom validator settings.

1) Add reference to Microsoft.AspNet.OData (check corresponding version in your bin folder).

enter image description here

2) Add your custom ServicesConfigurator with needed validation parameters:

public class ODataServicesConfigurator : IServicesConfigurator
{
    public void Configure(IServiceCollection serviceCollection)
    {
        ODataValidationSettings validationSettingFactory(IServiceProvider sp) => new ODataValidationSettings
        {
            MaxAnyAllExpressionDepth = 3,
            MaxExpansionDepth = 3
        };

        serviceCollection.AddSingleton(validationSettingFactory);
    }
}

3) Register your ServiceConfigurator in config:

<?xml version="1.0"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
    <sitecore>
        <services>
            <configurator type="YourNamespace.ODataServicesConfigurator, YourNamespace"/>
        </services>
    </sitecore>
</configuration>

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.