All About SharePoint

Liedong(Ken) Zheng,SharePoint Leader at SIMPLOT

Archive for April 3rd, 2012

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

Posted in Sharepoint | Tagged: | 1 Comment »

 
Follow

Get every new post delivered to your Inbox.

Join 28 other followers