I'm having trouble querying SharePoint in such a way, as to combine a view results with my own CAML query inside a WCF service. It seems GetItems
ignores my query, and just returns all the items in the view regardless.
Here's the sample code I'm trying out right now:
public bool Test()
{
using (SPSite site = SPContext.Current.Site)
{
using (SPWeb web = site.OpenWeb(SPContext.Current.Web.ID))
{
var list = web.Lists["Test"];
var caml = "<Where><Eq><FieldRef Name=\"Field2\" /><Value Type=\"Boolean\">1</Value></Eq></Where>";
var view = list.Views["TestView"];
var query = new SPQuery();
query.Query = caml;
List<SPListItem> viewWithCamlItems = new List<SPListItem>();
SPListItemCollection results;
do
{
results = list.GetItems(query, view.ID.ToString("B").ToUpper());
results.Cast<SPListItem>().ToList().ForEach(i => viewWithCamlItems.Add(i));
query.ListItemCollectionPosition = results.ListItemCollectionPosition;
} while (results.ListItemCollectionPosition != null);
var camlOnlyResults = list.GetItems(new SPQuery() { Query = caml }).Cast<SPListItem>().ToList();
var x = viewWithCamlItems.Count;
var y = camlOnlyResults.Count;
return x == y;
}
}
}
The list in question has only 2 columns: Field1 and Field2, and both of these are Yes/No columns. The view displays only those rows where Field1 is True. At the moment of writing this question, I've only got 1 row in the list where Field2 is True; considering the camlOnlyResults
contains just that one row I think the CAML is correct, and I'd expect x
and y
to be equal...
However, I get all rows in viewWithCamlItems
inside the view...
What am I doing wrong?
EDIT: The reason I'm using paging in one case and not the other, is because the view might have a small row limit, while the default SPQuery
has a row limit of uint.MaxValue
, which is plenty.