Introduction
Sharepoint CAML query is difficult to understand. Here I have provided a quick method to create CAML query.
Sharepoint
-
A caml
query is build in two parts.
-
Sort
part is the sorting based on some columns
-
Filter part is the where clause of the CAML query
-
In order to
build up the XML for the CAML we create the where clause as follows:
-
Write the where clause just as you write in sql
-
Find
out the operators such as =, <=, <,> etc.
-
Find
out the keywords for these operators in CAML language. For example = is eq, < is
lt and so on.
-
Break the sql string such that the operator comes first and then comes the
operands. Continue this till the whole string is completed in this fashion.
-
Replace the operators with the keywords in CAML language.
-
Example: where column1=”a” and column2=”b” and column3 like ‘%c%’
-
=>
where (column1=”a” and column2=”b”) and column3 like ‘%c%’
-
=>
where and (column1=”a” and column2=”b”) (column3 like ‘%c%’)
-
=>
where and (and (column1=”a” column2=”b”) column3 like ‘%c%’)
-
=>
where and and (= column1 a column2 b) contains column3 c
-
=>
where and and eq column1 a eq column2 b contains column3 c
-
=>
<where>
<and>
<and>
<eq>
<FieldRef name=”Column1” />
<Value Type=string>a</Value>
</eq>
<eq>
<FieldRef name=”Column2” />
<Value Type=string>b</Value>
</eq>
</and>
<contains>
<FieldRef name=”Column3” />
<Value Type=string>c</Value>
</contains>
</and>
</where>
Points of Interest
History
Keep a running update of any changes or improvements you've
made here.