Pages

27 Dec 2013

Get all Items from a list avoiding treshold

Code:
 private static List<SPListItem> GetAllListItems(SPWeb web, SPList list)
        {
            List<SPListItem> listItems  = new List<SPListItem>();
            SPQuery query = new SPQuery();
            SPListItemCollection spListItems;
            string lastItemIdOnPage = null;
         
            int itemCount = 2000;

            while (itemCount == 2000)
            {
                query.ViewFieldsOnly = false;
                query.RowLimit = 2000;
                if (lastItemIdOnPage != null)
                {
                    SPListItemCollectionPosition pos = new SPListItemCollectionPosition(lastItemIdOnPage);
                    query.ListItemCollectionPosition = pos;
                }
                spListItems = list.GetItems(query);

                if (spListItems.ListItemCollectionPosition == null)
                {
                    listItems.AddRange(spListItems.Cast<SPListItem>());
                    break;
                }

                lastItemIdOnPage = spListItems.ListItemCollectionPosition.PagingInfo;
                itemCount = spListItems.Count;
                listItems.AddRange(spListItems.Cast<SPListItem>());
            }
            return listItems;
        }

No comments:

Post a Comment