Ordering and Pagination

Ordering and pagination for results

There are a couple of query options that gives you control over your results and how they are ordered. With them you can not only able to create collection results that are sorted in the required order but to implement paging functionality using the $top and $skip options.

Ordering

With the $orderby property you are able to sort collection results by one or more properties and forward or reverse direction. You can specify as many fields as you want.

You can use the orderings option to order your results by a certain field. By default it will sort the field from the lowest value to the highest.

The following examples shows how to sort the results by the DisplayName field:

Copy
https://dev.demo.sensenet.com/OData.svc/Root/Content/IT/Document_Library?$orderby=DisplayName

Order by a field in an explicit direction

The following examples shows how to sort the results by the Id field from lowest to highest:

Copy
https://dev.demo.sensenet.com/OData.svc/Root/Content/IT/Document_Library?$orderby=Id asc

Order by a field in reverse order

The following example shows how to sort the results by the CreationDate field from closest to earliest:

Copy
https://dev.demo.sensenet.com/OData.svc/Root/Content/IT/Document_Library?$orderby=CreationDate desc

Order by a multiple fields

You can specify multiple fields to sort the order of your results by. The results will be ordered first by the first field specified. If any of those values are equal, then those results will be sorted by the next specified field. And so on.

Here is an example that will order the results first by the last modification date of the docs from closest to earliest. It will then sort any docs that have the same ModificationDate by their DisplayName.

Copy
https://dev.demo.sensenet.com/OData.svc/Root/Content/IT/Document_Library?$orderby=ModificationDate desc,DisplayName,Name

Top

With the $top option you can limit collection results.

Copy
https://dev.demo.sensenet.com/OData.svc/Root/Content/IT/Document_Library?$top=5
 

Skip

$skip option is for hiding the first given number of elements from the result.

Copy
https://dev.demo.sensenet.com/OData.svc/Root/Content/IT/Document_Library?$skip=5
 

Pagination

In case of paging the $top option defines the maximum number of documents that the API will return for your query so that the number of docs that will be listed on a page and $skip defines how many docs will be skipped.

Following example demonstrates how to query the second page of a folder list with 3 folders on a page.

Copy
https://dev.demo.sensenet.com/OData.svc/Root/Content/IT/Document_Library?$top=3&skip=3