Saturday, September 6, 2008

Retrieving list items with CAML

Scenario:
You want to retrieve all the items of list

Code:

SPQuery qry = new SPQuery();

string camlquery = "<OrderBy><FieldRef Name='Country' /></OrderBy><Where>"

+ "<Eq><FieldRef Name='LastName' /><Value Type='Text'>Sandeep</Value></Eq>"

+ </Where>";

qry.Query = camlquery;
Usage:
You can use the above 'qry' object in few different ways
SPListItemCollection listItemsCollection = list.GetItems(qry);


or

DataTable listItemsTable = list.GetItems(qry).GetDataTable();
Additonal Note: This query will return all the columns of the list, so if you are intrested in only a few of the columns then you can use

qry.ViewFields = "<FieldRef Name='FirstName' /><FieldRef Name='LastName' />";
Article:
http://sharepointmagazine.net/technical/development/writing-caml-queries-for-retrieving-list-items-from-a-sharepoint-list

0 comments: