Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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}",
                        item["FullName"]);
            }

            Console.WriteLine("Count: {0} | Count Query: {1} ",
                    items.Count,
                    items.List.ItemCount);
        }
    }
}

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

share|improve this question
1  
When all items are returned, that means that something's wrong with your Query... –  Ruben Herman Oct 17 '12 at 10:49
add comment

2 Answers

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.

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spquery.listitemcollectionposition.aspx

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

share|improve this answer
add comment

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 ?

share|improve this answer
    
Then the "Items.Count" is returning 2 I believe he is returning the value 2 because of Row Limit –  Tiago Oct 17 '12 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 Oct 17 '12 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 Oct 17 '12 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 Oct 17 '12 at 14:36
    
Thanks for the help! I thought it was quite easy to get this value. –  Tiago Oct 21 '12 at 15:01
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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