3

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
2
  • Are you certain your CAML is correct? What is the 'FieldRef Name="Status0"' refer to? Is this field actually called Status0? Have you tried hardcoding your ViewXml with a more strict query to be sure it works? Just a few things I have seen in the past. Nov 19 '12 at 14:51
  • The ViewQuery comes from the View (which in the Sharepoint UI returns the correct number of results) so I believe the ViewQuery CAML is correct and effective.
    – Mike
    Nov 19 '12 at 15:04
3

Using a sample CAML query from http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.client.list.getitems.aspx I noticed that their ViewXml looks different than mine. Specifically, it's wrapped in

<View> and <Query> 

tags. Modifying the XML I get back from the View:

        string viewQuery = string.Format("<View><Query>{0}</Query></View>", 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 }); 

it now works as desired.

1

The count before the query will always return the amount in the list, if you perform the count after the query it will limit to the number that was returned in the results.

6
  • I'm not sure I understand. How would I change the code to reflect your suggestion?
    – Mike
    Nov 19 '12 at 14:47
  • SPListItemCollection.List Gives you the parent object, you want the count of the filtered list, so I mean do the count on the list after the GetItems call. A good example of code is here in a previous question sharepoint.stackexchange.com/questions/49234/…
    – Hugh Wood
    Nov 19 '12 at 14:53
  • the list.ItemCount property is still 2107 at the end of my code snippet though (that's what you're referring to, right?)
    – Mike
    Nov 19 '12 at 15:06
  • No, I am referring it that you are using the collection object, not the filtered list. The link I provided has the code example in the answer on how it is done.
    – Hugh Wood
    Nov 19 '12 at 15:07
  • I've looked at the link you submitted, and the question in that link is basically the same as what I'm asking - however the suggestions there say call "list.GetItems(query).Count" which is exactly what I'm doing already. Would you be able to suggest how I could update my code to try your suggestion?
    – Mike
    Nov 19 '12 at 15:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.