SharePoint 2013: Using REST API For Selecting, Filtering, Sorting And Pagination in SharePoint List

Introduction

I have explained in the previous article how to work with SharePoint list items, basically performing CRUD operations, using the combination of REST API and jQuery Ajax. The REST URI ends with any OData query operators to specify selecting, sorting, or filtering.Let’s see other parameters and options which can be used with REST services. We will start from basic REST url and will take Selecting, filtering, sorting and pagination options one by one.

The main agenda of this Article is to understand how you can take selecting, filtering, sorting and pagination options one by one.

infolist

Scenarios

There can be scenarios where you want to perform additional operations on data returned by REST API like selecting, filtering, sorting and paginationfields only etc. For such operations while forming Url to Get data, we have to add some parameters as described below:

  • Selecting and sorting items

    $select

    This ‘’ /administration/_api/web/lists/getbytitle('infolist')/items’url returns all items with all possible fields or list columns. But what if list has more than 20-30 columns? It’s not good practice to get all fields. Get only specific fields which are required. You can select the specific attributes to return for your items using the $select parameter. Below example show how to use it.

    Syntax for this is $select=Field1, Field2, Field3
    /_api/web/lists/getbytitle('infolist')/items?$select=ID,Title,Employee,company

    xml

    '$orderby'

    If your response is a list of items, each with a number of field names, you can sort the list on a given field name using the $orderby, $orderby.asc or $orderby.desc system filter parameter.

    The first two specify sorting in ascending order and the third one descending order. It's simple, you can use '$orderby' parameter and provide the field name. REST service will return sorted list items in response.

    Syntax for this is $orderby=(Column Internal Name order)

    See below examples

    xml

Ascending Order

  1. /_api/web/lists/getbytitle('infolist')/items?$select=ID,Title,Employee,company&$orderby= Employee asc 
Descending Order
  1. /_api/web/lists/getbytitle('infolist')/items?$select=ID,Title,Employee,company&$orderby= Employee desc  
  • Filtering items:

    You can filter your list to contain only items which match a simple logical expression using the $filterparameter.

    Syntax: for this is $filter=(Column Internal Name operator value).
    See below examples,

    Filter by Title

    /_api/web/lists/getbytitle('infolist')/items?$filter=Employee eq ‘parth'

    Filter by ID:

    /_api/web/lists/getbytitle('infolist')/items?$filter=ID eq 2

    Filter by Date

    /_api/web/lists/getbytitle('infolist')/items?$filter=Start_x0020_Date le datetime'2016-03-26T09:59:32Z'

    Multiple Filters

    /_api/web/lists/getbytitle('infolist')/items?$filter=( Modified le datetime'2016-03-26T09:59:32Z') and (ID eq 2)

    Title name starts with the letter P

    /_api/web/lists/getbytitle(‘'infolist')/items?$filter=startswith(Title,‘P’)

    Return all items from the 'infolist'list modified in May

    /_api/web/lists/getbytitle(‘'infolist')/items? $filter=month(Modified) eq 5



    code

    OData query operators supported in the SharePoint REST service,

    table

  • Paging items

    The $top operators are used to implement paging for results. The $top operator specifies how many results to return.

    Syntax for this is $top Count. This returns top and records.

    See below example
    1. /_api/web/lists/getbytitle('infolist')/items?$top 5  

Note

The $skipoperator does not work in SharePoint 2013 on list items.it works only on Lists.

See below example

/_api/web/lists? Orderby Title desc&$skip

$expand

This is very useful when dealing with person or lookup fields where only Id is returned. Using this we can get corresponding value based on Id.

See below example

Lookup Field:Say there is City column in County list which is a lookup to Title column in Info List.

  1. /_api/web/lists/getbytitle('infolist')/items?$select=ID,Title,Employee,company,city/Id&$expand=city/Id  
People Field

Let’s say a list has custom field: Author, it will return ‘AuthorId’ in response. What is the proper way to deal with people field? You need to use ‘$expand’ parameter to expand the field.

Following REST URL gives your idea how to use $expand.
  1. /_api/web/lists/getbytitle('infolist')/items?$select=Author/Title&$expand=Author/Id  
Reference
Read more articles on SharePoint,