Saturday, November 22, 2014


I have explained how to achieve Pagination and Multiple Filters in REST Services in my last posts. Lets see other parameters and options which can be used with REST services. We will start from basic REST url and will take all options one by one.

Following is the simple REST url to get all items of a list. All you have to do is just give the Title if list as shown in below example:

/_api/web/lists/getbytitle('ListName')/items

'$select' :

Above url returns all items with all possible fields or list columns. But what if list has more than 20-30 columns? Its not good practice to get all fields. Get only those field which are required. To achieve this you can use '$select' parameter or option. Below example shows how to use it.

/_api/web/lists/getbytitle('ListName')/items?$select=ID,Title,Age,Name

With the help of select parameter you can tell service just get only those columns which are mentioned.

'$orderby' :

Now what if you want sorted list items. Simple, you can use '$orderby' parameter and provide the field name. REST service will return sorted list items in response.

/_api/web/lists/getbytitle('ListName')/items?$select=ID,Title,Age,Name&$orderby= ID

Above REST will return list item sorted by ID. Use 'desc' or 'asec' to sort them ascending or descending.

/_api/web/lists/getbytitle('ListName')/items?$select=ID,Title,Age,Name&$orderby=ID desc.

'$top' :

Now to get top list items you can use '$top' parameter. This parameter tells REST to return only specified top items in result.

/_api/web/lists/getbytitle('ListName')/items?$select=ID,Title,Age,Name&$orderby=ID desc&$top=5.

This is useful while showing latest items on screen like latest news, articles, announcements, blogs, etc.

'$skip' :

Now lets see how to skip certain items coming in response. To achieve this you can use. '$skip' parameter.

/_api/web/lists/getbytitle('ListName')/items?$select=ID,Title,Age,Name&$orderby=ID desc&$skip=5.

This REST URL skips top 5 records after order by on ID.

'$expand' :

The trickiest part in REST is dealing with people fields and lookup fields. Let’s see one by one.

People Field:

This field internally contains the ID of user from the User Information list. Whenever you make following call you get result like shown below for the User or People field. In this case it is Approver field.

/_api/web/lists/getbytitle('ListName')/items



Strangely it returns ‘ApproverId’! But the list does not have such field? No need to panic, as said People field internally contains id of user or person the REST service returns Approver field as ‘ApproverId’. So let’s say list have 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 you idea how to use $expand.

To get The Login name:

_api/web/lists/getbytitle('StudentsList')/items?$select=Name,Title,Approver/Name&$expand=Approver/Name



To get just Id:

_api/web/lists/getbytitle('StudentsList')/items?$select=Name,Title,Approver/Id&$expand=Approver/Id



Lookup Field:

The same is applicable for Look up field. To work with them you need to expand them. Examples as below:

_api/web/lists/getbytitle('StudentsList')/items?$select=Name,Title,Testlookup/Id&$expand=Testlookup/Id



Happy SharePoint coding.

1 comment:

  1. Knowledge about the latest and vital technology would increase one's self esteem to the core at the time of lagging confidence. The content presented here is quite resembling the same. You have done a great job by sharing this in here.

    Best JAVA Training institute in Chennai | Best JAVA Training in Chennai | Hadoop training in chennai

    ReplyDelete