CAML Basics

CAML (Collaborative Application Markup Language)

The CAML queries are internally used to query the SharePoint List with specific conditions and formatting. It is very powerful as SQL queries and improves the performance. The CAML query can be split into

  • Display Part
  • Conditional Part

Display Part

They are generally used to display the content in a grouped and ordered manner.

Eg: OrderBy, GroupBy

<Query>

  <OrderBy>

    <FieldRef Name='Name'/>

  </OrderBy>

  <GroupBy>

    <FieldRef Name='Location'/>

  </GroupBy>

</Query>

Conditional Format

They are used to retrieve data with certain conditions. Like using logical operators. The following example queries will give an overview.

1.Simple Condition

The condition (Name ==”Balaji”) in CAML form would look like

<Where>

  <Eq>

    <FieldRef Name='Name'/>

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

  </Eq>

</Where>

2.Multiple Condition

Multiple condition with Logical operators have a tricky part that each logical operator can have two parts more than two should have another logical operator to be defined encapsulating it.

The condition((Name ==”Balaji”) && (Location==”Chennai”) && (ID==1))in CAML form would look like

<Where>

  <And>

    <And>

      <Eq>

        <FieldRef Name='Name'/>

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

      </Eq>

      <Eq>

        <FieldRef Name='Location'/>

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

      </Eq>

    </And>

    <Eq>

      <FieldRef Name='ID'/>

      <Value Type='Number'>1</Value>

    </Eq>

  </And>

</Where>

Normal Operator SharePoint  specific
= Eq
> Gt
< Lt
>= Geq
<= Leq
Like Contains
Null Check IsNull
Not Null NotNull
Begins BeginsWith
Date Range DateRangesOverlap
&& And
|| Or

Generally CAML queries can be easily generated using several tools. Refer the following link for more info http://msdn.microsoft.com/en-us/library/ff648040.aspx

SP Value types http://msdn.microsoft.com/en-us/library/aa558695(BTS.20).aspx