While I was building a CAML query for retrieving files from a document library, I used the U2U CAML query tool to construct the query syntax. It worked fine in the tool and returned the proper data, but when I used that query in the code, I found that ALL files were returned and not just the ones I expected.
As it turns out, the U2U CAML Query tool adds the tag <Query> to the query although it should not be included when using it to query items from a SPList. (Note: There are some instances where the Query tag is required, but for this case, it is not)
Here is a sample query:
SPListCollection spBPDLListCollection = spBPDLWeb.Lists;
SPList splist = spBPDLListCollection[documentLibraryName];
SPQuery query = new SPQuery();
// CAML for looking up documents owned by current user
query.Query = “<Where><Contains><FieldRef Name=’Author’ /><Value Type=’User’>”
+ currentDisplayName + “</Value></Contains></Where>“;
// get the items specified with the query
SPListItemCollection items = splist.GetItems(query);