4

How can I get all list items created or changed by the currently logged in user with this function? What's the correct query?

// Getting list items based on ODATA Query
function getListItems(url, listname, query, complete, failure) {

    // Executing our colors ajax request
    $.ajax({
        url: url + "/_api/web/lists/getbytitle('" + listname + "')/items" + query,
        method: "GET",
        headers: { "Accept": "application/json; odata=verbose" },
        success: function (data) {
            complete(data); // Returns JSON collection of the results
        },
        error: function (data) {
            failure(data);
        }
    });

}

3 Answers 3

12

Use the $filter query option with Author property to filter items by Modified field:

$filter=AuthorId eq <UserId>

How to retrieve current user via SharePoint REST API

If the current user is not determined, then it could be requested using the following method:

function getCurrentUser(url,complete, failure)
{    
    $.ajax({
        url: url + "/_api/web/currentUser",
        method: "GET",
        headers: { "Accept": "application/json; odata=verbose" },
        success: function (data) {
            complete(data.d); 
        },
        error: function (data) {
            failure(data);
        }
    });
}

Afterwards, you could retrieve my list items as demonstrated below.

Example: retrieve my list items using SharePoint REST API

function getMyListItems(url, listname,complete, failure) {
  getCurrentUser(url,
    function(user){
       var query = '?$filter=AuthorId eq ' + user.Id;    
       getListItems(url,listname,query,complete,failure); 
    },
    failure
  );
}

References

Use OData query operations in SharePoint REST requests

5
  • 1
    You can generally get the current user via _spPageContextInfo.userId. Saves you having to make a service call. Apr 8, 2014 at 1:59
  • 1
    Thanks Rob, indeed, in most cases there is no need to make an additional request for getting current user Id. Apr 8, 2014 at 7:18
  • 3
    I don't have a column 'AuthorId' but I can use 'Author/Id'
    – jlai
    Apr 8, 2014 at 7:48
  • 1
    Nice. But to get user Id without any request please have look at Akhoy's answer here: sharepoint.stackexchange.com/questions/153571/… Aug 31, 2017 at 3:07
  • @jlai79 I know this is old, but for the benefit of those coming upon this thread... The CreatedBy field has the underlying field name of Author. But that field has 3 parts, the Id, Name, and Email. When using the filter to access them, you don't use slashes or dots, they just run them together in the JSON that is returned: AuthorId is correct in the filter that is supplied here.
    – vapcguy
    Aug 15, 2019 at 19:32
1

I don't believe there is much you can do since the list only stores the User Id. Rather than querying the UserInformationList for the Id, you can grab it from _spPageContextInfo if you are doing this from the client side.

<script type="text/javascript">
  $(document).ready( function(){ 
    var userId = _spPageContextInfo.userId; 
    var restURL = url + "?$filter=AuthorId eq " + userId;

    $.ajax({        
      dataType: "json",       
      url: restURL,       
      success: function(data) {  
        //iterate through json objects and output 'my items' to a table 
      }     
    });  
  });  
</script>
0
//Display items created by user id 1

var currentUserId  = 1;

var url = SPAppWebUrl + "/_api/SP.AppContextSite(@target)" +
        "/web/lists/getbytitle('" + listName + "')/items?$orderby=OfficeLocation,Department,FirstName&$filter=AuthorId eq " + currentUserId + "&" + "@target='" + SPHostUrl + "'";

    $.ajax(
        {
            url: url,
            type: "GET",
            contentType: "application/json;odata=verbose",
            headers: { "Accept": "application/json;odata=verbose" },

            success: function (data) {
            var result = data.d.results;

                for (var i = 0; i < result.length; i++) {

    alert(result[i].FirstName);
            alert(result[i].LastName);
           }
        }

        error: function (err) {
                alert(JSON.stringify(err));
            }
        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.