0

I have a SharePoint list with two columns:

users (type people, multiple values allowed)
responsible_department (type string)

I want to get the items from this list where the current user is in the ùsers` field. The field can have multiple users (multiple users allowed)!

I am currently able to get the current user:

    var currentUser;
    function init() {
        this.clientContext = new SP.ClientContext.get_current();
        this.oWeb = clientContext.get_web();
        currentUser = this.oWeb.get_currentUser();
        this.clientContext.load(currentUser);
        this.clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
    }

    function onQuerySucceeded() {
        console.log(currentUser.get_loginName());
    }

    function onQueryFailed(sender, args) {
        console.log('Request failed. \nError: ' + args.get_message() + '\nStackTrace: ' + args.get_stackTrace());
    }

No i need to query the mutli user field in my list for all items where my current user is part of the people field. I dont know how to query for this.

Can someone help me out?

STORM
  • 4,005
  • 11
  • 49
  • 98

1 Answers1

0

I found a solution on MSDN and this works for me:

<script src="//code.jquery.com/jquery-3.1.0.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
 var siteURL = _spPageContextInfo.webAbsoluteUrl;
 var listname = "CustomList";
 var currentUserId=_spPageContextInfo.userId;
 var url = siteURL + "/_api/web/lists/getbytitle('" + listname + "')/items?$select=Title,PeopleField/ID&$filter=substringof('"+currentUserId+"',PeopleField/ID)&$expand=PeopleField/ID";
 $.ajax({
     url: url,
     method: "GET",
     headers: { "Accept": "application/json; odata=verbose" },
     success: function (data) {      
        var items = data.d.results;
        for(var i = 0; i < items.length;i++) {           
            var item=items[i];
            console.log(item.Title);
        }       
     },
     error: function (data) {       
     }
 });
});
</script>
STORM
  • 4,005
  • 11
  • 49
  • 98