First off, we need to explain what CAML stands for – – Collaborative Application Markup Language. It’s an XML based query language in order to perform a query operation against data in SharePoint.

 There are several sections that are used within CAML.

  • View – the section which usually surrounds all of the other sections
  • Query – includes OrderBy and Where sections (used inside View section)
  • ViewFields – the actual list of fields that are displayed from the query (used inside View section)
  • RowLimit – Sets the limit for the number of items returned from the query, per page. (used inside View section)
  • Where – similar to the filtering done by SQL (used inside Query section)
  • Or/And – if you have multiple items in your Where section, create each, using Comparison Operators and FieldRefs/Values, etc inside either an Or or and And section.
  • OrderBy – uses FieldRef to define the sort order of the query results which are returned (like SQL)
  • GroupBy – Also uses FieldRefs to group the data returned from a query in a list view.

 Here’s a list of comparison operators used in CAML:

Comparison Operators Meaning
Eq =  (Equals)
Gt >  (Greater than)
Lt < (Less than)
Geq >= (Greater than or equal to)
Leq <= (Less than or equal to)
Neq <>  (Not Equal)
Contains Contains Text (Like)
IsNull Is Null
IsNotNull Is Not Null
BeginsWith Beginning with word/text
DateRangesOverlap Dates in recurring event with a specified value

 

To use a CAML query in your C# code, use something like the following:
SPWeb web = SPContext.Current.Site.RootWeb;
SPList mylist = web.Lists["myList"];
SPQuery query = new SPQuery();
query.Query = "<Where><Eq><FieldRef Name='Status'/><Value Type='Text'>Completed</Value></Eq></Where>”;
SPListItemCollection items = myList.GetItems(query);

foreach (SPListItem item in items)
{
// Show Items from collection however you’d like
}