The U2U CAML Query tag

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);

  1. Leave a comment

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: