Rest API filter list items created by logged in user SharePoint

Here we will extend our previous article where we had discussed How to make synchronous Rest API call in SharePoint online using jQuery? Here we will discuss: Rest API filter list items created by logged in user SharePoint online or SharePoint 2013 / Rest API filter list items created by ME / How to get items from SharePoint Online list by the current logged in user using Rest API? / get list items created by logged in user with rest API SharePoint 2013 SharePoint Online /SharePoint rest API expand author / Get Created by value filter in Rest API filter SharePoint online / sharepoint rest expand user field etc.

We will also discuss how to get Logged in user details using Rest API SharePoint Online/2013/2016. We will retrieve logged in user details like Email, Display name, Id etc, using Rest API in SharePoint Online or SharePoint 2013/2016.

Rest API filter list items created by logged in user SharePoint

Here I have a SharePoint online Announcement list which has a Title column and few data has been inserted by various users. The list looks like below:

rest api filter
rest API filter

Now our requirement here is to retrieve items based on rest API filter on the created by field. We wanted to display items created by the current logged in user. Here we are using Rest API to filter items based on the current logged in user.

By using the _spPageContextInfo.userEmail we are retrieving the current logged in user email Id.

In the rest endpoint URL, we are filtering based on current user email id which we are comparing with the Created By’s email id field. The internal name of Created By column in “Author”.

We can place the below code inside a script editor web part, which can be added into a web part page.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js" ></script>
<h1>Training Announcements</h1><br/>
<div id="result"></div>
<script>
var LoginName=_spPageContextInfo.userEmail;
var newURL="/_api/Web/Lists/GetByTitle('TrainingAnnouncements')/Items?$select=Title,ID,Author/EMail&$filter=Author/EMail eq '"+ LoginName +"'&$expand=Author/Id";
jQuery().ready(function () {
function getItems(url) {
return $.ajax({
url: _spPageContextInfo.webAbsoluteUrl + url,
type: "GET",
headers: {
"accept": "application/json;odata=verbose",
}
});
}
getItems( newURL).done(function(data){
var fullresult="";
data.d.results.forEach(function(item){ // no need for oldskool for loops
fullresult+=item.Title +'<br><br>';
});
jQuery('#result').html(fullresult);
});
});
</script>

Once you add the code, you can see the rest api filter records. If Logged in as user Bijay, you can see the items showing which are created by Bijay.

rest api filter example sharepoint
rest api filter example sharepoint

If logged in with Bhawana, you can see the items created by Bhawana.

rest api filter sharepoint online example
rest api filter sharepoint online example

Get list items created by logged in user with REST api in SharePoint Online

This is another approach, how to get list items created by logged in user with Rest API in SharePoint Online. Below is the Rest API code to filter list items created by the current logged in user in SharePoint Online.

In this SharePoint Rest API example, I have a SharePoint Online custom list as Department is there. We will retrieve items created by the logged in user from the department list.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js" ></script>
<h1>Departments Added by Logged In User</h1><br/>
<div id="result"></div>
<script>
var userId = _spPageContextInfo.userId;
var newURL="/_api/Web/Lists/GetByTitle('Departments')/Items?$filter=AuthorId eq " + userId;
jQuery().ready(function () {
function getItems(url) {
return $.ajax({
url: _spPageContextInfo.webAbsoluteUrl + url,
type: "GET",
headers: {
"accept": "application/json;odata=verbose",
}
});
}
getItems( newURL).done(function(data){
var fullresult="";
data.d.results.forEach(function(item){ // no need for oldskool for loops
fullresult+=item.Title +'<br><br>';
});
jQuery('#result').html(fullresult);
});
});
</script>

I have added the code inside a script editor web part and you can see the results like below:

Get list items created by logged in user with REST api in SharePoint Online
Get list items created by logged in user with REST api in SharePoint Online

Get Logged in user details using Rest API SharePoint Online/2013/2016

We can easily retrieve current logged in user details using Rest API in SharePoint Online or SharePoint 2013/2016. Below is the rest API code to retrieve logged in user details using Rest API in SharePoint Online.

The Rest end point to retrieve current logged in user details:

https://onlysharepoint2013.sharepoint.com/sites/SharePointSky/_api/web/currentuser

Similarly, if we want to retrieve specific properties like Email, LoginName, Title, UserID etc, then we need to modify the endpoint URL to like below:

https://onlysharepoint2013.sharepoint.com/sites/SharePointSky/_api/web/currentuser?$select=Email,LoginName,Title,Id

By using the above rest end point we can retrieve only selected properties.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js" ></script>
<input type="button" id="btnClick" value="Get User Details using Rest API"/>
<script>
$(function(){
$("#btnClick").click(function(){
GetUserDetails();
});
});
function GetUserDetails() {
var url = "https://onlysharepoint2013.sharepoint.com/sites/SharePointSky/_api/web/currentuser";
$.ajax({
url: url,
headers: {
Accept: "application/json;odata=verbose"
},
async: false,
success: function (data) {
alert("User Name: "+data.d.Title +" Email ID: " +data.d.Email+" User ID: " +data.d.Id);
},
eror: function (data) {
alert("An error occurred. Please try again.");
}
});
}
</script>

Once you run the above code, you can see the logged in user details like below:

Get Logged in user details using Rest API in SharePoint
Get Logged in user details using Rest API in SharePoint

SharePoint Online: Get Logged in user details using Rest API

Here is another approach, we can get logged in user details using Rest API in SharePoint Online.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js" ></script>
<h1>Logged In User Details</h1><br/>
<div id="result"></div>
<script>
var newURL="/_api/web/currentuser";
jQuery().ready(function () {
function getLoggedInUserDetails(url) {
return $.ajax({
url: _spPageContextInfo.webAbsoluteUrl + url,
type: "GET",
headers: {
"accept": "application/json;odata=verbose",
}
});
}
getLoggedInUserDetails(newURL).done(function(data){
var fullresult="";
fullresult+="User Name: "+data.d.Title +"<br><br>";
fullresult+="Email ID: "+data.d.Email +"<br><br>";
fullresult+="ID: "+data.d.Id +"<br><br>";
jQuery('#result').html(fullresult);
});
});
</script>

Once you run the above code, you can see the logged in user details like below in SharePoint Online/2013/2016.

Get Logged in user details using Rest API in SharePoint
Get Logged in user details using Rest API in SharePoint

Read some SharePoint Rest API tutorials:

I hope this SharePoint Online tutorial explains, how to filter list items based on the current logged in user using Rest API in SharePoint Online.

We also saw how to retrieve logged in user details like User name, Email, ID using Rest API in SharePoint Online or SharePoint 2013/2016.

  • Very Helpful article.

    One question regarding Filter option with REST API… Can we use “IN” operator to find items matching comma separated options.

    I want to retrieve items from SharePoint list with Status “Open” and “Pending”

  • >