Rest API Filter Methods On SharePoint List Items

In this blog we will see how to filter SharePoint list items using Rest API. We can filter items based on field title, field ID or modified date of items.  In Rest API through  url parameter we can easily perform filtering on SharePoint list items. For filter methods url parameter is composed of three components.

  • Field name
  • Operator such as eq,lt,le,gt,ge,ne
  • Value for filtering

In this blog we have a custom list “Employee”. We will filter the employee name having age greater than 30.

Add a script editor on your home page. Go to edit mode and these below codes in script editor webparts.

<script src=”https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.2.1.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].First_x0020_Name +’\n’;
}
alert (result);
}
function onError(error) {

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

<input type=”button” id=”btnClick” value=”Filter List Items By Title(Age greater Than 30)”/>

After saving the page you will see a button has been added to the homepage.

Result:

Click on the button displays an alert showing all employee’s ID and name having age greater than 30.

Keywords:

Applying Rest API filter query method on Sharepoint List Items
Sharepoint Using Rest API for filtering Sharepoint List Items
SharePoint Rest API Filter Example
Rest API Filter Conditions on Sharepoint
Rest API Filter Methods On SharePoint ListItems

Leave a Reply

Your email address will not be published. Required fields are marked *