Wednesday, February 16, 2011

CAML Query and Where Clause Examples

Here I will try to cover as many combinations of Where Clauses as I can.
I will also explain each of them a little. Keep visiting this post for more examples.


EQ: If you notice here, we don't have any "=" (equals to) sign. We have our equals to criteria wrapped inside EQ Tag.
<Where>
<Eq>
<FieldRef Name='Title'/>
<Value Type='Text'>Hello World</Value>
</Eq>
</Where>

AND: If we want to have data with a criteria which involves 2 or more column and a AND condition, this is how it looks in CAML. Notice we have all criteria nested inside AND
<Where>
<And>
<Eq>
<FieldRef Name='Title'/>
<Value Type='Text'>Hello World</Value>
</Eq>
<Eq>
<FieldRef Name='Column2'/>
<Value Type='Text'>CAML</Value>
</Eq>
</And>
</Where>

OR: OR schema is same as AND
<Where>
<Or>
<Eq>
<FieldRef Name='Title'/> <Value Type='Text'>Hello World</Value>
</Eq>
<Eq>
<FieldRef Name='Title'/><Value Type='Text'>Bye World</Value>
</Eq>
</Or>
</Where>

Greater Than: Any such operator can be used with or without AND/OR Clause
<Where>
<Gt>
<FieldRef Name='Age'/><Value Type='Text'>18</Value>
</Gt>
</Where>

Less Than: Any such operator can be used with or without AND/OR Clause
<Where>
<lt><FieldRef Name='Salary'/><Value Type='Text'>$5000</Value></lt>
</Where>

Order By: This clause is used independent of Where clause. If you do not define an order, default is ascending.
<OrderBy>
<FieldRef Name="Salary" Ascending="True"/>
<FieldRef Name="Age" Ascending="False"/>
<FieldRef Name="JoiningDate"/>
</Order>
<!--[if !supportLineBreakNewLine]-->
<!--[endif]-->

1 comment:

KansasCoder said...

Good Stuff!
What if I have something like
"Give me A where x=y AND (B=1 OR C=1 OR D=1)

Thanks!