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>

4 Answers 4

21

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>
7
  • Also no spaces around the equal sign. Name = " becomes Name=".
    – Kit Menke
    Commented Jul 15, 2013 at 3:58
  • cleaned those up Commented Jul 15, 2013 at 5:32
  • 1
    Spaces around the equal sign i.e. Name = " do not create any issue. Commented Mar 15, 2015 at 12:53
  • <FieldRef> don't want a body!!! The answer of @ilMattion is right.
    – Emaborsa
    Commented Jul 11, 2016 at 8:05
  • @Emaborsa what are you referring to? Commented Jul 11, 2016 at 11:56
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>
1

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

2
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>

Your Answer

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

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