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.
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="https://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:
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:
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:
- Create a custom User profile web part using SharePoint REST API and bootstrap
- Retrieve and Display TASK status using REST API in SharePoint
- Create a Custom People Directory using REST API in SharePoint
- Create a Custom Calendar in SharePoint using Rest API and jQuery
- Display SharePoint list data in jQuery data table using Rest API
- SharePoint Rest API Crud Operations
- Send Email with jQuery and REST API in SharePoint
- Delete SharePoint List Item attachment using Rest API
- Upload attachment to a new item on the list using REST API JQuery in SharePoint 2013
- SharePoint Rest API: Create or Delete folder from a document library
- Value does not fall within the expected range in SharePoint Rest API
Hope this will be helpful to know how to filter list items using rest api in SharePoint.
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 SPGuides.com