Friday, July 17, 2009

Using Queries to open files

This is pretty basic information, but I had trouble finding a simple code snippet outlining how this works.  Here is super short snippet that finds and opens a file by it's Title field. 

//Begin by opening your List
SPList docList = web.Lists["Requests"];

//Build your query.  In this example, fileName is the name of a file we're already saved to the list
SPQuery query = new SPQuery();
query.Query = "<Where><Eq><FieldRef Name='Title' /><Value Type='Text'>" + fileName + "</Value></Eq></Where>";

//Now use your query to pull all the matching documents from our list. 
SPListItemCollection listItemColl = docList.GetItems(query);

//In this example, we're assumign that we'll always only get a single document back. 
//In practice, you should check listItemColl.Count to ensure that you're finding the doc.
 SPListItem listItem = listItemColl.List.Items[0];



The meat of the snippet is the Query string: <Where><Eq><FieldRef Name='Title' /><Value Type='Text'>" + fileName + "</Value></Eq></Where>
I'd highly recommend downloading U2U's CAML Builder to simplify building your query.  Just remember to strip out the <Query> tag U2U's tool likes to insert.

No comments: