Hi,
To do a CAML query in a SharePoint list you can use the class SPQuery. Here's an example.
Code:
try
{
SPSite site = new SPSite("serveur_url");
SPWeb web = new site.OpenWeb();
SPList list = web.Lists["votre_liste"];
SPQuery query = new SPQuery(list.DefaultView);
query.Query = " Une Valeur ";
SPListItemCollection items = list.GetItems(query);
foreach (SPListItem item in items)
{
Console.WriteLine(item["Title"]));
}
}
catch(SPException SpEx) { //Traitement d'erreur }
Here we use the operator "<Eq>, we could eg use <Contains a
search corresponding to a SQL LIKE. You must clearly specify the
internal name of the field list and not the title "Title". There are
also graphical tools to generate CAML queries. A little research on CAML
Query Builder on google guide you to this type of tool.
To specify which fields to include in a CAML query
Code:
try
{
SPSite site = new SPSite("serveur_url");
SPWeb web = new site.OpenWeb();
SPList list = web.Lists["votre_liste"];
SPQuery query = new SPQuery(list.DefaultView);
query.Query = " Une Valeur ";
SPListItemCollection items = list.GetItems(query);
foreach (SPListItem item in items)
{
Console.WriteLine(item["Title"]));
}
}
catch(SPException SpEx) { //Traitement d'erreur }
In the previous example, we specify the view "DefaultView". We could
have used a view defined in the list. It is also possible to specify
fields to include as follows:
Code:
query.ViewFields = "<FieldRef Name='Champ1'/><FieldRef Name='Champ2'/>....";
Follow these libks-
http://rmanimaran.wordpress.com/2011/03/11/new-in-sharepoint-2010-caml-query/
http://praveenbattula.blogspot.com/2010/02/sharepoint-querying-caml-query-usage.html
Hope this will help you