Thursday, October 21, 2010

How to write Nested Query in CAML


It's strange that CAML query can only compare a pair of expressions either by <And> or <Or>. So if you have a query like you want to filter <And> condition between two <Or> condition, then you need to use nested <And> or <Or>, in which each operator only takes two expressions. This will make things more complicate especially for a number of conditions mixed with <And> and <Or>.

Example :

<Query>
   <Where>
      <And>
         <Geq>
            <FieldRef Name="Expires" />
            <Value Type="DateTime"><Today/>Value>
         </Geq>
      <Or>
         <Eq>
            <FieldRef Name="SubCategory" />
            <Value Type="Choice">PreLoginValue>
         </Eq>     
         <Eq>
            <FieldRef Name="SubCategory" />
            <Value Type="Choice">BothValue>
         </Eq>
      </Or>
    </And>
   </Where>
</Query>

Thanks.

Special Thanks to Mr.Abhay Wad.

No comments:

Post a Comment