12

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

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

We're looking for long answers that provide some explanation and context. Don't just give a one-line answer; explain why your answer is right, ideally with citations. Answers that don't include explanations may be removed.

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, privacy policy and cookie policy

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