I am filtering a list in caml

SPQuery oQuery = new SPQuery();
oQuery.Query = "<OrderBy><FieldRef Name='FullName' /></OrderBy><Where><Contains><FieldRef Name='FirstName' /><Value Type='Text'>e</Value></Contains></Where>";

using (SPSite siteCollection = new SPSite("http://localhost/"))
    using (SPWeb web = siteCollection.OpenWeb())
        SPList oList = web.Lists["Profile"];
        SPListItemCollection items = null;

        oQuery.ViewFields = "<FieldRef Name='FullName'/>";
        oQuery.RowLimit = 2;

        items = oList.GetItems(oQuery);

        if (items.Count > 0)
            foreach (SPListItem item in items)
                Console.WriteLine("Full Name: {0}",

            Console.WriteLine("Count: {0} | Count Query: {1} ",

I'm using the SPListItemCollection.List.ItemCount, who is returning the value of 50 records. However records are 50 total items on my list, and I would like to know the total records on this list with the filter that actually are 22.

What is the best method to find this value? Thanks

  • 1
    When all items are returned, that means that something's wrong with your Query... Commented Oct 17, 2012 at 10:49

2 Answers 2


This is not working for you?

SPQuery query = new SPQuery() { Query = "<OrderBy><FieldRef Name='FullName' /></OrderBy><Where><Contains><FieldRef Name='FirstName' /><Value Type='Text'>e</Value></Contains></Where>" };
SPList oList = web.Lists["Profile"];
SPListItemCollection items = oList.GetItems(query);
int count = items.Count;

Please let me know

Actually you are getting the count in a wrong way, SPListItemCollection.List gives you the parent SPList item, on this whole List you are getting ItemCount which will essentially gives you the total number of items in the list, why are not you directly getting the count as items.Count ?

  • Then the "Items.Count" is returning 2 I believe he is returning the value 2 because of Row Limit
    – Tiago
    Commented Oct 17, 2012 at 11:10
  • obviously, why do you need Row Limit if you want to get the count of filtered items? remove the row limit and you will get 22 (or whatever actual value) as count
    – Diptarag
    Commented Oct 17, 2012 at 13:01
  • So the problem is that I have to use the Row Limit, because I'm making a list with paging, and believe this way for performance, has no way of knowing the total items using RowLimit?
    – Tiago
    Commented Oct 17, 2012 at 14:02
  • the method quickly pops on my mind first dont set rowlimit and give a call to count the number, and after you get the count, set rowlimit to the same SPQuery variable and call again, not great method but will work surely, will post if anything better comes in my mind
    – Diptarag
    Commented Oct 17, 2012 at 14:36
  • Thanks for the help! I thought it was quite easy to get this value.
    – Tiago
    Commented Oct 21, 2012 at 15:01

If you are looking to limit the number of items returned by the server object model and at the same time build some kind of paging UI, use SPQuery.ListItemCollectionPosition property.


This following article cover its usage in details: http://apmblog.compuware.com/2009/01/15/sharepoint-page-through-sharepoint-lists/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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