Sorting In Sitecore Item Web API

As you know Sitecore Item Web API  is pretty handy to manipulate Sitecore items via HTTP request.
Recently we got a requirement to apply sorting based on custom field while returning items to Sitecore Item Web Api.

First I looked into Sitecore.ItemWebApi.config to find any setting for sorting,but didn’t find anything.

After that I started digging in to Sitecore.ItemWebApi.dll — one of the assemblies that ships with Sitecopre Item Web API.

After looking into the code,I decided to write a new pipeline processor  that will sort items based on the field name passed in query string.

Here is my code for sorting the items,which I added to the itemWebApiRead pipeline:

public override void Process(ReadArgs arguments)
    Assert.ArgumentNotNull(arguments, "arguments");
    arguments.Scope = GetSortedResult.GetSortedResultSet(arguments.Scope);
private static Item[] GetSortedResultSet(Item[] scope)
   Assert.ArgumentNotNull(scope, "scope");
   string queryString = WebUtil.GetQueryString("sort", null);
   string orderQueryString = WebUtil.GetQueryString("orderby", null);
   if (queryString == null && orderQueryString == null)
      return scope;
   if (queryString != null && orderQueryString != null)
     if(orderQueryString.ToLower() == "asc")
          scope = scope.Where(i => i.Fields[queryString] != null).OrderBy(i =>              ((Sitecore.Data.Fields.DateField)i.Fields[queryString]).DateTime).ToArray();

      else if(orderQueryString.ToLower() == "desc")
         scope = scope.Where(i => i.Fields[queryString] != null).OrderByDescending(i => ((Sitecore.Data.Fields.DateField)i.Fields[queryString]).DateTime).ToArray();
   else if(queryString != null)
      scope = scope.Where(i => i.Fields[queryString] != null).OrderByDescending(i => ((Sitecore.Data.Fields.DateField)i.Fields[queryString]).DateTime).ToArray();
   return scope;

The above pipeline processor class sort items based on passed query sting values. We can apply sorting on Sitecore standard datetime fields (__created, __updated etc.) and our custom date field on item.

Config file for our custom pipeline processor.

<configuration xmlns:patch="">
                <processor     patch:before="processor[@type='Sitecore.ItemWebApi.Pipelines.Read.GetResult, Sitecore.ItemWebApi']"
type="Sitecore.ItemWebApiSort.Pipelines.Read.GetSortedResult, Sitecore.ItemWebApiSort" />


Now to get sorted result,we just need to pass sort and orderby attributes in query strings as:
1. sort – field name (we need to supply our field name here).
2. orderby – sort order(desc or  asc ).

We can now get sorted results as below:

With our custom date field in descending order – http://{sitename}/-/item/v1/?sc_itemid={itemID}&scope=c&sc_database=master&sort=date&orderby=desc

With our custom field in ascending order – http://{sitename}/-/item/v1/?sc_itemid={itemID}&scope=c&sc_database=master&sort=date&orderby=asc

With standard Sitecore field(__created) in descending order:

With Sitecore standard field (__created) in ascending order: http://{sitename}/-/item/v1/?sc_itemid={itemID}&scope=c&sc_database=master&sort=__created&orderby=asc

That’s it for now, please let me know your thoughts in comments below.

This entry was posted in Sitecore and tagged , , , , , , , . Bookmark the permalink.

2 Responses to Sorting In Sitecore Item Web API

  1. Pingback: Sitecore: Interview questions | Sitecore Tweaks

  2. tmamedbekov says:

    Thank you for this. I am trying to implement it in 8.2 I am having one problem.

    public void Process(ReadArgs arguments)
    Assert.ArgumentNotNull(arguments, “arguments”);
    arguments.Scope = GetSortedResult.GetSortedResultSet(arguments.Scope);

    GetSourtedResultSet underlines as an error.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s