12

I'm having some trouble getting a CAML Query working against a very simple list.

My custom list has the fields:

  • Person : User
  • Date : Date
  • Amount : Lookup

My query looks like this:

<Query>
  <Where>
    <Eq>
      <FieldRef Name='Person' LookupId='TRUE' />
      <Value Type='Integer'>{0}</Value>
    </Eq>
  </Where>
</Query>

and {0} contains an integer.

Problem

My list contains 8 elements, divided between two seperate users. My query is supposed to get me 4 elements back, but is always returning 8.

I have been researching this for quite some time now (using both U2U Query Builder, and trying to handwrite it) but have been unable to get it to filter.

Please advise!

Solution

The solution was to remove the <Query></Query> from the CAML Query-string. The below accepted answer doesn't adress this, though it is also correct.

28

Try to use this caml:

<Where>
  <Eq>
    <FieldRef Name="Author" LookupId="True" />
    <Value Type="User">123</Value>
  </Eq>
</Where>

this also might work, but first is preferable:

<Where>
  <Eq>
    <FieldRef Name="Author" LookupId="True" />
    <Value Type="Lookup">123</Value>
  </Eq>
</Where>

and also, if you want to pass current logged user into caml query, you can use this one:

<Where>
  <Eq>
    <FieldRef Name="Author" />
    <Value Type="Integer">
      <UserID />
    </Value>
  </Eq>
</Where>
  • 3
    Nice tip there on the last one, didn't know that. – Eric Alexander May 11 '12 at 12:33
  • Here is what mine looks like. <Where><Eq><FieldRef Name="Author" LookupId="True" /><Value Type="User"><UserID /></Value></Eq></Where> – phil Mar 27 '17 at 13:37
  • how to pass a spuser variable ? like i am saving the spuser string strnp11user =@"mydomain\NPD11"; SPUser objuser1 = spWeb.EnsureUser(strnp11user); how to pass this objuser1 inside the caml query? i – prasadwt May 17 '17 at 4:11
  • Here's another tip: the third example has whitespace in the <Value> element. But if you do that in example 1 or 2, it won't work. – johnnycardy Apr 12 '18 at 13:54
0

if you are using the system account (probably in your development environment (Virtual Machine)), the does not work as expected.

The user Purna said the same thing in this post.

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.