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.

2

2 Answers 2

27

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>
4
  • 3
    Nice tip there on the last one, didn't know that. Commented May 11, 2012 at 12:33
  • Here is what mine looks like. <Where><Eq><FieldRef Name="Author" LookupId="True" /><Value Type="User"><UserID /></Value></Eq></Where>
    – phil
    Commented Mar 27, 2017 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
    – dasarp
    Commented May 17, 2017 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. Commented Apr 12, 2018 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.

0

Your Answer

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