Hi, i’ve noticed the default sort order for the Delivery API has changed from sortOrder to createdDate, is there a way to revert this behavior.
Thanks.
Hi, i’ve noticed the default sort order for the Delivery API has changed from sortOrder to createdDate, is there a way to revert this behavior.
Thanks.
Hi @jam ,
There isn’t anything provided directly but we can do few things from our side to achieve this:
The easiest way is to explicitly define the sort order in your front-end fetch requests. If you pass the exact parameter, it overrides the default fallback.
Just add this to your query string: ?sort=sortOrder:asc
Example: GET /umbraco/delivery/api/v2/content?sort=sortOrder:asc
The other way which is a server side fix, is to write a decorator for IApiContentQueryProvider. When a request hits the server without any sorting rules, your decorator intercepts it and injects sortOrder before Umbraco processes the query.
Here is a rough idea of what that code looks like (Have asked AI to generate code for this, Haven’t tried it, please check once.):
using Umbraco.Cms.Core.DeliveryApi;
// other using statements...
public class CustomSortQueryDecorator : IApiContentQueryProvider
{
private readonly IApiContentQueryProvider _innerProvider;
public CustomSortQueryDecorator(IApiContentQueryProvider innerProvider)
{
_innerProvider = innerProvider;
}
public PagedModel<Guid> ExecuteQuery(SelectorOption selectorOption, IList<FilterOption> filterOptions, IList<SortOption> sortOptions, string culture, ProtectedAccess protectedAccess, bool preview, int skip, int take)
{
// If the API request doesn't specify a sort, force it to use sortOrder
if (sortOptions == null || !sortOptions.Any())
{
sortOptions ??= new List<SortOption>();
sortOptions.Add(new SortOption
{
FieldName = "sortOrder",
Direction = Direction.Ascending
});
}
// Pass the modified options back to the default handler
return _innerProvider.ExecuteQuery(selectorOption, filterOptions, sortOptions, culture, protectedAccess, preview, skip, take);
}
}
You will need to register this in your Program.cs or a Composer so the Dependency Injection container picks it up instead of the default provider.
Documentation Link: If you want to look further into modifying how the API handles requests, check out the official docs on custom sorting and filtering here: Umbraco Extension API for Querying
Hope it helps!
Regards,
Shekhar