Friday, March 20, 2009

SPQuery not executing the query and returning all results

Problem Statement

I was writing some code using SPQuery and came across a problem where the query was returning me all the list items instead of only the filtered items.

SPQuery query = new SPQuery();

string qry = String.Format(“<'Query'><'Where'><'Eq'><'FieldRef Name=\”Field1\” /'><'Value Type=\”User\”'>{0}<'/Value'><'/Eq'><'/Where'><'/Query'>”, “Ranish”);
query.Query = qry;

SPListItemCollection results = MyList.GetItems(query);

Console.WriteLine(“Result Count: “ + results.Count);

Solution

Remove the <"Query">..<"/Query"> tag in the CAML string and the query will work fine. Silly mistake from my part…I copied the query directly from U2U CAML Builder that’s why!! Need to be more cautious next time

Cheers

d[-_-]b

No comments:

Post a Comment