2

I need to build a query a custom list with a user field. The where clause filters the results to a given list of user login names of a dynamic length, so I build the query according to the numbers of users.

My problem is that executeQueryAsync returns every item of the list, no matter how simple the where clause is.

Example for the simplest query:

<View><Query>
   <Where>
     <Eq>
       <FieldRef Name="UserName"/>
       <Value Type="User">domain\username</Value>
     </Eq>
   </Where>
</Query></View>

What could be wrong?

2

You cannot use usernames in CAML queries to filter items based on the values of a Person field. You need to use either the full name or the id of the user.

If you only have the username, then you should get the user id first using the username and then execute the query to get list items using the user id that you got earlier.

Your code should look something like this:

var context = SP.ClientContext.get_current();
var web = context.get_web();
var user = web.ensureUser('DOMAIN\\USERNAME');

context.load(user);
context.executeQueryAsync(function() {

    var list = web.get_lists().getById('LIST_ID_HERE');
    var query = new SP.CamlQuery();
    var queryXml = '<View><Query><Where><Eq><FieldRef Name="Author" LookupId="TRUE"/><Value Type="Integer">' + user.get_id() + '</Value></Eq></Where></Query></View>';

    query.set_viewXml(queryXml);
    var items = list.getItems(query);

    context.load(items);
    context.executeQueryAsync(function() {

        // The list items has been retrieved.

    }, function() {});

}, function() {});

Your implementation may be a bit different, but you get the idea.

  • Thanks, I know that. The problem is that I may have hundreds of users where I need to request the user IDs of, which would take too long. Maybe the initial question should have been: How do I get the IDs of a list of users with just one server request. – elsni May 29 '17 at 15:29
  • All site users will be stored in root site "UserInformationList". Which might help you get all users. You can try if it helps. My request is like this `siteurl/_vti_bin/listdata.svc/UserInformationList' – Venkat Konjeti May 29 '17 at 15:57
  • Your answer is the first place on the internet where I saw how to use a CAML query in JSOM. Thanks! (The documentation for viewXml only made sense after reading your post.) – jpaugh Sep 20 '17 at 15:33
  • @elsni You can batch multiple calls to web.ensureUser in the same request, as long as you stay under the request limit. There's no reason you can't loop over every username, and do context.load(web.ensureUser(username), 'UserId'). – jpaugh Sep 20 '17 at 15:45
2
<Query>
   <Where>
     <Eq>
       <FieldRef Name="UserName"/>
       <Value Type='Integer'><UserID Type='Integer'/></Value>
     </Eq>
   </Where>
</Query>
  • I only have the login name. I tried to query the hidden UserInfo list for the IDs but I also got all Items back. – elsni May 29 '17 at 12:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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