Tuesday, May 5, 2015

CAML Queries For SharePoint Fields

Collaborative Application Markup Language (CAML) is an XML based markup language to define queries against list data. You can use CAML query to get the filtered, grouped or sorted data from SharePoint list.  CAML query can be used to retrieve SharePoint data in SharePoint object model, Web Service as well as PowerShell.
Following is the basic structure of CAML query:

Followings are the operators to use under Where element:

Logical Operators

And Or are the logical operators you can use to filter the data on multiple fields.

Comparison Operators

Operator Meaning
Eq = (Equal To)
Neq <> (Not Equal To)
Lt < (Less Than)
Gt > (Greater Than)
Geq >= (Greater Than Or Equal To)
Leq <= (Less Than Or Equal To)
Contains Like
IsNull Null
IsNotNull Not Null
BeginsWith Beginning with the word
DateRangesOverlap Compare the dates in a recurring event with a specified
DateTime value, to determine whether they overlap

Order/Group Operators

Operator Meaning
OrderBy Specify the sort order. Query can be sort by multiple fields.
GroupBy Specify the grouping for data. Data can be group by multiple fields

Followings are few snippets of CAML query to filter on the different types of fields in SharePoint. 

Text, Choice, Number, Currency, Boolean

Replace the DataType in the query. Followings are the datatypes:
DataType Field
Text Single line of text
Choice Choice field (menu to choose from)
Number Number field (1, 1.0, 100)
Currency Currency field ($, ¥, €)
Boolean Yes/No field (check box)
1 = Yes
0 = No

Date and Time

DateTime datatype can be used with multiple options. It can work with provided Date/DateTime or time part Today

Using Today




By Name