So I have a list of items (e.g. FAQs). and I am calling them through the WEB Api. I want to be able to do a get from Sitecore and be able to sort based on the date created and sort it either ascending or descending. Is there a work-around, or there is something OOB?
-
3Please expand the question with code samples of where you are now, and what isn't giving you what you'd like.– Mark Cassidy ♦Commented Feb 1, 2017 at 0:16
-
1Have you tried sorting your list using linq after you got them using the Web API?– DiegoCommented Feb 1, 2017 at 13:36
-
@MarkCassidy I just want to make a call to SiteCore and get items, and sort them based on a specific field name (e.g. date created, date modified etc.).– TonyCommented Feb 1, 2017 at 19:17
-
@Diego is there any reference, or maybe an article how to do this?– TonyCommented Feb 1, 2017 at 19:18
-
It's not possible to sort items OOTB with Sitecore Web API. To achieve it you would need to create a custom processor and patch itemWebApiRead pipeline. Alternatively you could look into using Sitecore's ItemService which provides REST API and supports sorting.– grgCommented Feb 2, 2017 at 16:38
2 Answers
It's possible to extend although not entirely that easy because Sitecore uses a lot of dynamics instead of actually typing things when it comes to ItemWebApi.
A small example (note that this kinda works but isn't intended to actually be used, more like a starting point):
public class SortResult : ReadProcessor
{
#region Overrides of OperationProcessor<ReadArgs>
public override void Process(ReadArgs arguments)
{
// Gets the sorting parameter.
// This needs to exist in the parameters list found underneath processor type="Sitecore.ItemWebApi.Pipelines.Request.CheckParameters, Sitecore.ItemWebApi"
var sortingParameter = arguments.Context.HttpContext.Request.Params["sorting"];
if (string.IsNullOrWhiteSpace(sortingParameter)) return;
var getResult = arguments.Result as Dictionary<string, object>;
var result = getResult?["result"] as Dictionary<string, object>;
var items = result?["items"] as Sitecore.ItemWebApi.Dynamic[];
if (items != null)
{
// Sort the result using the key provided.
var sortedItems = items.OrderBy(item => item[sortingParameter]);
result["items"] = sortedItems.ToArray();
}
}
#endregion
}
And the config showing where to patch it:
<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<pipelines>
<itemWebApiRead>
<processor type="ItemWebApi.Pipelines.Read.SortResult, ItemWebApi"
patch:after="processor[@type='Sitecore.ItemWebApi.Pipelines.Read.GetResult, Sitecore.ItemWebApi']" />
</itemWebApiRead>
</pipelines>
</sitecore>
</configuration>
This works but I think it might be easier just to overwrite GetResult
inside the itemWebApiRead
pipeline instead of going through the hassle of changing the dynamics.
If the WEB Api returns the same order as items sorted in the content tree you can change it by:
- Select an item folder where all sub-items required to be sorted by some criteria.
- On the Home Ribbon click "Sorting" and change sort order to created.
Before:
Result: