Is there any way to return only the count of the rows from a Sharepoint query? I would like to get only the count of the result returned by sharepoint query.


If you use CAML to get list items (which is the fastest way of retrieving items), it is just enough to get ids of items to be able get the count:

//some query
var title = "<Eq><FieldRef Name='Title' /><Value>task 00001</Value></Eq>";
var q = "<Where>" + title + "</Where>";
var lst = web.Lists["Tasks"];
var query = new SPQuery
    ViewFields = "<FieldRef Name='ID'/>",
    Query = q,
    IncludePermissions = false,
    RowLimit = 20000
var items = lst.GetItems(query);
var count = items.Count;
  • 1
    +1 using viewfields with a query is the only way. There is no "Count" function. – Kit Menke Jan 3 '12 at 15:18

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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