10

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.

8

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 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.