Monday, March 30, 2009

CAML query with more than two conditions

CAML queries seem to have a weird restriction that prevents you from using more than 2 conditions in a row. For example, this query doesn’t work:

 

<Query>

   <Where>

      <And>

            <Eq>

               <FieldRef Name='Title' />

               <Value Type='Text'>A</Value>

            </Eq>

            <Eq>

               <FieldRef Name='_UIVersionString' />

               <Value Type='Text'>B</Value>

            </Eq>

           <Eq>

              <FieldRef Name='FirstName' />

              <Value Type='Text'>C</Value>

           </Eq>

      </And>

   </Where>

</Query>

 

In the above you will notice that there are three “AND” conditions (the <Eq>’s) in a row so we need to wrap the condition in groups of two and nest the conditions like this:

 

<Query>

   <Where>

      <And>

         <And>

            <Eq>

               <FieldRef Name='Title' />

               <Value Type='Text'>A</Value>

            </Eq>

            <Eq>

               <FieldRef Name='_UIVersionString' />

               <Value Type='Text'>B</Value>

            </Eq>

         </And>

         <Eq>

            <FieldRef Name='FirstName' />

            <Value Type='Text'>C</Value>

         </Eq>

      </And>

   </Where>

</Query>

 

Miguel figured this one out recently and kindly shared.....

1 comment: