All About SharePoint

Liedong(Ken) Zheng,SharePoint Leader at SIMPLOT

Value does not fall within the expected range. Iterating SPListItems

Posted by ken zheng on April 3, 2012

I got this error when I used SPQuery to get all list items and check the version count.

foreach (SPListItem item in myItems)
{

item.Versions.Count

}

after a while searching, you have to change your code like

SPListItem itemCurr = item.ParentList.GetItemById(item.ID);

 

Below is my code to count all documents including versions

SPDocumentLibrary docLib = (SPDocumentLibrary)oSPWeb.Lists[docLibName];

                        Console.WriteLine("Library Name: " + docLib.Title);

                                              

                        SPQuery query = new SPQuery();

                        query.ViewAttributes = "Scope=\"RecursiveAll\"";

                        SPListItemCollection myItems = list.GetItems(query);

                        Console.WriteLine("total Items: " + myItems.Count);

                        foreach (SPListItem item in myItems)
                        {
                            if (!IsFolder(item))
                            {

                                SPListItem itemCurr = item.ParentList.GetItemById(item.ID);
                                Console.WriteLine("item count value: " + itemCurr.Versions.Count);
                                total = total + itemCurr.Versions.Count;
                            }
                        }
About these ads

One Response to “Value does not fall within the expected range. Iterating SPListItems”

  1. MagicGuru said

    Don’t think this is the root cause. It could be that ViewFieldsOnly=true for your query object. Or Mucrosoft decides it better be true for performance reason.

    I can run item.Versions.Count for a small document library. It returns version count without issue.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

Join 28 other followers

%d bloggers like this: