Recently I received a number of emails regarding the SPQuery class and the way we can use it to search for items in a WSS list using a CAML string. Here is an excerpt out of my 'Building SharePoint Solutions' course covering that part:
It is possible to avoid the extensive use of loops to find the items you need within your code. SharePoint exposes an SPQuery class representing a query in a list view.
Queries to the content database can be executed using this class in combination with the Collaborative Application Markup Language (CAML). CAML is an XML-based language that is used in Microsoft Windows SharePoint Services to define sites and lists, including, for example, fields, views, or forms. We are not going to use CAML for doing these types of tasks however. We will use it to create query strings. The importance of CAML is also weakened in version 2 of SharePoint where most of the action is occuring through ASP.NET controls on the server. In v1, CAML was used heavily during the page execution. This is not true anymore.
When you know the structure of a list, it is possible to construct a CAML XML string and execute this string by first creating a SPQuery object, assigning the CAML string to the Query property and next execute the query using the GetItems() method of the SPList object.
How to find out the structure of the CAML string you need to construct?
Suppose you need to find the email of a certain trainer. In our case we can easily create a small loop like we have done before, but it could be that there are hundreds of items in a list. Doing a search is in that case a better approach.
The first you need to find out is the structure of the view that you want to query. You can actually query a view instead of the list itself. So, after getting a reference to an SPList, you can create a SPQuery object for a specific view in the list.
sharePointAdmin = new SPGlobalAdmin();
SPVirtualServer vServer =
sharePointAdmin.OpenVirtualServer(new Uri("http://neelix:200/"));
SPSite site = vServer.Sites["Team Web Site"];
SPWeb web = site.OpenWeb();
SPList trainersList = web.Lists["U2U Trainers"];
SPQuery query = new SPQuery(trainersList.Views["All Items"]);
The SPQuery object has a property ViewXml returning the necessary information to create your CAML string.
MessageBox.Show(query.ViewXml);
<View System.Xml.XmlAttributeCollection><Query></Query><ViewFields><FieldRef Name="LinkTitleNoMenu" /><FieldRef Name="Email" /><FieldRef Name="Topics" /></ViewFields><RowLimit>100</RowLimit></View>
We can use this information to construct the following CAML string:
string caml = "<Where><Eq><FieldRef Name='LinkTitleNoMenu'/>" +
"<Value Type='Text'>Patrick Tisseghem</Value></Eq></Where>";
The only thing to do now is the assign this string to the Query property of the SPQuery object and execute the query by passing it as an argument to the GetItems() method of the SPList object.
query.Query = caml;
SPListItemCollection results = trainersList.GetItems(query);
SPListItem trainer = (SPListItem)results[0];
MessageBox.Show(trainer["Email"].ToString());