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