Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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>
share|improve this question

3 Answers 3

up vote 3 down vote accepted

Try this:

<OrderBy>
<FieldRef Name="ID" Ascending="TRUE"/>
</OrderBy>
<Where>
 <And>
  <Eq>
   <FieldRef Name="HomeType"><Value Type="Choice">Condo</FieldRef>
  </Eq>
  <Eq>
   <FieldRef Name="LastName"><Value Type="Text">Doe</FieldRef>
  </Eq>
 </And>
</Where>
share|improve this answer
    
Also no spaces around the equal sign. Name = " becomes Name=". –  Kit Menke Jul 15 '13 at 3:58
    
cleaned those up –  PirateEric Jul 15 '13 at 5:32

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

share|improve this answer

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.

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

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>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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