I have a Sharepoint List called "Tracker" that represents customer-submitted defects. I have a View on this list that only shows the items created in the last 7 days. This view works just fine when I use Sharepoint using a web browser. I want to get the count of this View using C#.
Here's the code I've attempted, but the viewCount always comes out the same as the List's item count. What am I missing?
string webUrl = "https://mysharepointsite";
ClientContext context = new ClientContext(webUrl);
List list = context.Web.Lists.GetByTitle("Tracker"); // Should have 2107 items in it
var view = list.Views.GetByTitle("Trackers Created in Last 7 Days"); // Should have 4 items in it
context.Load(list);
context.Load(view);
context.ExecuteQuery();
int listCount = list.ItemCount; // count = 2107
string viewQuery = view.ViewQuery; // <Where><And><Neq><FieldRef Name="Status0" /><Value Type="Text">Closed</Value></Neq><Geq><FieldRef Name="Created" /><Value Type="DateTime"><Today OffsetDays="-7" /></Value></Geq></And></Where>
var listItemCollection = list.GetItems(new CamlQuery { ViewXml = viewQuery });
context.Load(listItemCollection);
context.ExecuteQuery();
int viewCount = listItemCollection.Count; // count = 2107 - this is bad, expected a value of 4