In this article, we will understand the basics of CAML query in SharePoint.
What is CAML?
Ø CAML - Collaborative Application Markup Language
Ø XML- Extensible Markup Language based query language
Ø Used to perform a query operation against SharePoint Lists
How SharePoint List Items are retrieved?
SharePoint List data can be retrieved in any one of the following ways:
1. Using the SharePoint object model – used when code runs on the server (Example: Developing a web part or an application page)
2. Using the SharePoint Lists web service – used when your code doesn’t run on the server where the SharePoint is installed (Example: Developing a windows application)
3. Using Power shell –used mostly by the ADMIN of the SharePoint when they quickly want to retrieve some information from the SharePoint site
How does CAML query looks like?
As I already mentioned, it is XML based query language and it contains tags in it. The root element of the CAML query root element is Query. But it is not necessary to use Query element in the query you form.
Within the Query element you have two elements possible:
1. Where – to filter the data
2. OrderBy – to categorize the data
A simple structure of the CAML query is as follows:
<Query>
<Query>
<Where>
<Eq>
<FieldRef Name=”FieldName” />
<Value Type=”DataType”>Value</Value>
</Eq>
</Where>
<OrderBy>
<FieldRef Name=”FieldName” />
<FieldRef Name=”FieldName” />
</OrderBy>
</Query>
Operators in CAML Query
From the above structure, we came to know that it uses Where and OrderBy elements to retrieve the data from the list.
Let us know about the operators present in the CAML query and its usage:
Inside the Where element
1. Logical Operators - AND, OR
2. Comparison Operators - Listed Below in the table
AND – Which takes two conditions are satisfied
Inside the OrderBy/GroupBy element
OrderBy – Which orders or sort the data depends upon the field (FieldRef element) given.
GroupBy – Which groups the data depends upon the group by field (FieldRef element) given.
Examples
Logical & Comparison Operators
Use of AND, Gt, Leq
<Query>
<Where>
<And>
<Gt>
<FieldRef Name="Quantity" />
<Value Type="Number">0</Value>
</Gt>
<Leq>
<FieldRef Name="Price" />
<Value Type="Number">2000</Value>
</Leq>
</And>
</Where>
</Query>
Use of OR, Gt, Leq
<Query>
<Where>
<Or>
<Gt>
<FieldRef Name="Quantity" />
<Value Type="Number">0</Value>
</Gt>
<Leq>
<FieldRef Name="Price" />
<Value Type="Number">2000</Value>
</Leq>
</Or>
</Where>
</Query>
Use of BeginsWith, Leq
<Query>
<Where>
<And>
<BeginsWith>
<FieldRef Name="Title" />
<Value Type="Text">M</Value>
</BeginsWith>
<Leq>
<FieldRef Name="Quantity" />
<Value Type="Number">1000</Value>
</Leq>
</And>
</Where>
<OrderBy>
<FieldRef Name="Price" Ascending="False" />
</OrderBy>
</Query>
OrderBy Operator
<Query>
<Query>
<Where>
<Or>
<Gt>
<FieldRef Name="Quantity" />
<Value Type="Number">0</Value>
</Gt>
<Leq>
<FieldRef Name="Price" />
<Value Type="Number">2000</Value>
</Leq>
</Or>
</Where>
<OrderBy>
<FieldRef Name="Price" Ascending="True" />
</OrderBy>
</Query>
Is it possible to write this queries without any errors manually?
Yes. But we will know the errors only after executing the program. Hence there is a free CAML query builder which will generate the query easily.
To know about where to download and how to use the CAML Query builder read this article.
To know how to use the CAML query in the Microsoft Visual Studio read this article.
So.. how do you actually execute the query?
ReplyDeleteYou can use CAML query builder to execute the query which is explained in this post
Deletehttp://sharepoint-works.blogspot.com/2012/05/using-caml-query-builder-for-sharepoint.html
Or
You can use it in MS Visual Studio to execute the query
http://sharepoint-works.blogspot.com/2012/05/caml-query-to-get-data-from-sharepoint.html
Hi Maruthachalam, nice article for beginners. You can also use the U2U CAML Builder see the link for more details http://tad.co.in/?p=468.
ReplyDeleteThanks for taking the time. is appreciated
ReplyDeleteYou are welcome!
DeleteSo with all the other query options that you've listed, why would you learn and use CAML? Indeed why did they create CAML in the first instance? It's ugly, it's like programming using XML (not much different to XSLT). The question is why? I would appreciate any insight on this.
ReplyDeleteHi zhuo,
DeleteRead this article to know why CAML query is used in SharePoint for querying the data from the Lists and Libraries.
Ref: http://msdn.microsoft.com/en-in/library/ee536691(v=office.14).aspx
Hope this answers your question.
To know how the data is queried from SharePoint read this article
Ref: http://sharepoint-works.blogspot.in/2012/05/caml-query-to-get-data-from-sharepoint.html
very easy...is that..haha..enjoy it...
ReplyDeleteRegards TanujKhurana
Hi..Nice article.
ReplyDeleteDo you know if it is possible to use CAML to query for items based on the tags?(http://office.microsoft.com/en-in/sharepoint-server-help/use-tags-and-notes-to-share-information-with-colleagues-HA101810816.aspx)
I did not find any way to obtain only items that have a specific tag.
Good article for beginners! thanks! Krishnamurthy :)
ReplyDeleteYou are welcome!
DeleteHi Guys, Thanks to all for this good article
ReplyDeleteOne thing need to include here which will be helpful for all.
Let us assume the name of custom list column is "State Code List" (with space in between)
How do we keep in . Please explain with solution
do we put this query caml code into aspx ? please assist..
ReplyDeleteYes. Read this article to know more
Deletehttp://sharepoint-works.blogspot.com/2012/05/caml-query-to-get-data-from-sharepoint.html
Thank you for taking the time to write this. Just two points.
ReplyDeleteThe popups are making my brain want to listen to Justin Beiber recite Trump speeches.
Using Right Quotation Mark unicode is troublesome. Please consider using '