14

Hi I am trying to build a query using caml query builder - Below is the query (The query = get the values only whose address = 1234st AND LastName = Doe) - I believe the placement of AND operator is giving me error, tried various permutation by changing the position of AND operators but no luck - any suggestions?

<OrderBy>
 <FieldRef Name = "ID" Ascending = "TRUE"/>
</OrderBy>
<Where>
 <And>
  <Eq>
   <FieldRef Name = "HomeType"><Value Type="Choice">Condo</FieldRef>
  </Eq>
 <And>
  <Eq>
   <FieldRef Name = "LastName"><Value Type="Text">Doe</FieldRef>
  </Eq>
 </And>
 </And>
</Where>
mbauer
  • 597
  • 1
  • 7
  • 21
SandeshR
  • 1,992
  • 17
  • 67
  • 115

4 Answers4

20

Try this:

<OrderBy>
<FieldRef Name="ID" Ascending="TRUE"/>
</OrderBy>
<Where>
 <And>
  <Eq>
   <FieldRef Name="HomeType"/><Value Type="Choice">Condo</Value>
  </Eq>
  <Eq>
   <FieldRef Name="LastName"/><Value Type="Text">Doe</Value>
  </Eq>
 </And>
</Where>
Eric Alexander
  • 43,293
  • 10
  • 53
  • 93
4

I think that this code:

<Eq>
 <FieldRef Name = "HomeType"><Value Type="Choice">Condo</FieldRef>
</Eq>

must be like that:

<Eq>
   <FieldRef Name = "HomeType" /><Value Type="Choice">Condo</Value>
</Eq>
ilMattion
  • 275
  • 5
  • 13
1

You had an extra <And> and </And>. Just remember that you only need a set of those around every 2 fields (or groups) you are comparing. Ex.

<Where>
 <And>
     <And>
       <Eq>
         <FieldRef Name="HomeType"><Value Type="Choice">Condo</FieldRef>
       </Eq>
       <Eq>
         <FieldRef Name="LastName"><Value Type="Text">Doe</FieldRef>
       </Eq>
     </And>
     <Eq>
        <FieldRef Name="ID"><Value Type="Integer">1</FieldRef>
     </Eq>
   </And>
</Where>

And this is also valid. Notice the grouping of two items per 'And', then an 'And' between the two groups:

<Where>
 <And>
     <And>
       <Eq>
         <FieldRef Name="HomeType"><Value Type="Choice">Condo</FieldRef>
       </Eq>
       <Eq>
         <FieldRef Name="LastName"><Value Type="Text">Doe</FieldRef>
       </Eq>
     </And>
     <And>
      <Eq>
        <FieldRef Name="Rooms"><Value Type="Integer">2</FieldRef>
      </Eq>         
      <Eq>
        <FieldRef Name="Location"><Value Type="Text">Brooklyn</FieldRef>
      </Eq>
     </And>
   </And>
</Where>
vapcguy
  • 308
  • 2
  • 10
1

Try using the CAML query buider from U2U, it's really helpful. It will build the queries for you

  • U2U CAML query builder link to download is dead now. I use CAML designer (freeware) from http://www.camldesigner.com/. Works for both 2010 and 2013 and pretty intuitive way to build queries. – Falak Mahmood Jul 15 '13 at 08:25
  • CAML query builder links question is answered: http://sharepoint.stackexchange.com/a/72165/62 – SPDoctor Jul 16 '13 at 08:30