What is CAML Query
The CAML language has been associated with SharePoint since the first version, SharePoint 2001, and SharePoint Team Services. It is based on a defined Extensible Markup Language (XML) document that will help you perform a data manipulation task in SharePoint. It is easy to relate a list to CAML if you compare it to a database table and query. When querying a database, you could get all of the records back from the table and then find the one that you want, or you can use a Structured Query Language (SQL) query to narrow the results to just the records in which you are interested.
Example: CAML Query Basic Example:
<Query>
<Where>
<!--Comparison Operators here-->
<Eq>
<FieldRef Name=”insertFieldNameHere” />
<Value Type=”insertDataTypeHere”>insertValueHere</Value>
</Eq>
</Where>
<OrderBy>
<FieldRef Name=”insertFieldNameHere” />
<FieldRef Name=”insertFieldNameHere” />
</OrderBy>
</Query>
Using CAML query in SharePoint Programming:
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
namespace CamlQuery
{
class Program
{
static void Main(string[] args)
{ "string camlQuery =
"" +
"" +
"" +
"" +
"Chen" +
"" +
"" +
"";
SPSite site = new SPSite("http://SPS123/");
SPWeb testWeb = site.AllWebs["Budget Test Site"];
SPList bdcList = testWeb.Lists["List with BDC Lookup"];
SPQuery query = new SPQuery( );
query.Query = camlQuery;
SPListItemCollection filteredItems =
bdcList.GetItems(query);
Console.WriteLine("Found {0} items in query.",
filteredItems.Count);
foreach (SPListItem item in filteredItems)
{ Console.WriteLine("Found customer: {0} {1}",
item["Customer"],
item["Customer: LastName"]);
} Console.ReadLine();
} } }"
CAML Query Comparison Operators:
Tag Name Meaning
Contains Contains a given text value
Eq Equal to
Geq Greater than or equal to
Gt Greater than
Leq Less than or equal to
Lt Less than
Neq Not equal to
DateRangesOverlap Compares dates in recurring events to determine if they overlap
IsNotNull Is not null
IsNull Is null
The following code is an example to extract the FieldRef name from a list by using a console application and the Microsoft Office SharePoint Server 2007 application programming interface (API):
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
namespace SharePointUtils
{
class Program
{
static void Main(string[] args)
{
string siteUrl = args[0];
string listName = args[1];
string viewName = args[2];
SPSite site = new SPSite(siteUrl);
SPWeb web = site.OpenWeb();
SPList employeesList = web.Lists[listName];
SPQuery query = new SPQuery(employeesList.Views[viewName]);
System.Diagnostics.Debug.WriteLine(query.ViewXml);
Console.WriteLine(query.ViewXml);
Console.ReadLine();
}
}
}
Some important points about CAML Query:
->CAML query is used to perform operation on SharePoint list. ->CAML query's root element is Query.
->Where claause in CAML query is used for filter criteria. ->Order by element is used to set the elements in a specific order either by ascending or descending.
Retrieving List items from SharePoint list using CAML query.
Setting the row limit in CAML query result set.
We can set the row limit in a result set of caml query. This can be done by using SPQuery. SPQuery spqry = new SPQuery(); spqry.RowLimit = 20;
Questions about SharePoint CAML Query:
Write a caml query to get the customer details from SharePoint list called Customer.
How to fetch record from a sharepoint list using caml query, the condition is only active user record should come in result set.
What is the difference between caml query and linq in sharepoint?
Which is good caml query or linq ?
Can caml query is applicable on sharepoint document library ?.
No comments:
Post a Comment