SharePoint Rest API: Filter list items based on conditions

This SharePoint tutorial explains, how to filter list items based on conditions in SharePoint Online or SharePoint 2013/2016.

If you are new to Rest API SharePoint, read SharePoint rest api tutorial and examples.

SharePoint Online rest api filter list items

Here I have a SharePoint custom list which 3 columns name as EmpID, FirstName, and Age. We will try to retrieve records whose Age is greater than 30 by using rest api in SharePoint.

sharepoint rest api filter items
sharepoint rest api filter items

We can put the below rest api code inside a script editor web part or content editor web part in SharePoint web part page.

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>

<script>
$(function(){
$("#btnClick").click(function(){
var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getByTitle('Employee')/items/?$filter=Age gt 30";
$.ajax({
url: requestUri,
type: "GET",
headers: {
"accept":"application/json; odata=verbose"
},

success: onSuccess,
error: onError
});

function onSuccess(data) {
var items = data.d.results;
var result=";
for (var i = 0; i < items.length; i++) {
result +=items[i].Id+ " : " + items[i].Title +'\n';
}
alert (result);
}
function onError(error) {

alert(JSON.stringify(error));
}
});
});
</script>

<input type="button" id="btnClick" value="Get all List Items based on Condition"/>

The results will come like below:

sharepoint rest api filter list items
sharepoint rest api filter list items

Similarly, if you want to retrieve records based on conditions like Age greater than 20 and less than 32

var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getByTitle('Employee')/items/?$filter=Age gt 20 and Age lt 33";

The results will come like below:

sharepoint 2013 rest api filter list items
sharepoint 2013 rest api filter list items

Similarly, If you want to filter based on date, then you can write the URL like below:

var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getByTitle('Employee')/items/?$filter=Created lt '2015-03-22'";

You may like following SharePoint rest api tutorials:

Hope this will be helpful to know how to filter list items using rest api in SharePoint.

free sharepoint training

SharePoint Online FREE Training

JOIN a FREE SharePoint Video Course (3 Part Video Series)

envelope
envelope

Bijay Kumar

I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site SharePointSky.com

>