Saturday, November 15, 2014


SharePoint 2013 has come with various new features; which are really exciting and one of them is REST APIs. REST APIs are very helpful to build client side solutions, you can use them to build a SharePoint App and any client side component. They are very simple and easy to use which helps to speed up the development. You can test them on the browser itself before writing any piece of code.


Main agenda of this blog is to understand how you can use multiple filters in query while fetching List item from list. Though it looks difficult at first look it is very simple to use.

To get items from list you can use following URL :
http://[SiteUrl]/_api/web/lists/getbytitle('ListName')/items

For example with above URL(/_api/web/lists/getbytitle('StudentsList')/item) I got  data like below.





Now what if you want to see items having certain values in field? To get only those list items with certain field values you can use filters. You can use filter in REST query like below:

http://[SiteUrl]/_api/web/lists/getbytitle('ListName')/items?$filter=[FieldName] eq 'value'.

For example with above URL (/_api/web/lists/getbytitle('StudentsList')/items?$filter=Class eq '1st') I got  data like below.





Now what if you want to apply filter on multiple fields? You will certainly think about putting ‘&&’ or ‘AND’ operators between two filters like below:

http://[SiteUrl]/_api/web/lists/getbytitle('ListName')/items?$filter=[FieldName1] eq 'value1' AND  [FieldName2] eq 'value2'

OR

http://[SiteUrl]/_api/web/lists/getbytitle('ListName')/items?$filter=[FieldName1] eq 'value1' &&  [FieldName2] eq 'value2'

But they are wrong. You cannot construct multiple filter query like above examples, the correct approach to construct multiple filter query is like below:

http://[SiteUrl]/_api/web/lists/getbytitle('ListName')/items?$filter=(([FieldName1] eq 'value1') and  ([FieldName2] eq 'value2')).

http://[SiteUrl]/_api/web/lists/getbytitle('ListName')/items?$filter=((([FieldName1] eq 'value1') and  ([FieldName2] eq 'value2')) or([FieldName3] eq 'value3)).

For example with above URL (/_api/web/lists/getbytitle('StudentsList')/items?Select=Name,Class,Age,City&$filter=((Class eq '2nd') and(Age eq '28')))/item) I get  data like below.





Likewise you can add more filters in REST query according to your requirement.

Happy SharePoint coding.

0 comments:

Post a Comment