Get All Items from a List
Get all list items from a SharePoint list can be very tricky in SharePoint development. First you need to consider if you want the "all items" including folders or not. Second, for larget list you won't be able to get all items at once because of throttling.
All Items and Folders
For beginners in SharePoint development, to get all items from a SharePoint list they may simply use "SPList.Items" as
foreach (SPListItem item in currList.Items)
{
cbItemList.Items.Add(item.ID);
}
There are several issues with this code. First, it won't get "Folder" items in the list. Second, for larget list it will fail because of throttling.
A second attemp to get all items may look like
SPQuery query = new SPQuery();
SPListItemCollection allItems = currList.GetItems(query);This code has issue too. It will only return items from root folder of the list. The result will also contain "Folder" items.
foreach (SPListItem item in currList.Items)
{
cbItemList.Items.Add(item.ID);
}
To Get All Items From All Sub-Folders Recursively Without Sub-Folders
you can use next without considering the throttling (which you should. I will address this later).
1. Use SPList.Items
foreach (SPListItem item in currList.Items)2. Use query.ViewAttributes = "Scope=\"Recursive\"";
{
cbItemList.Items.Add(item.ID);
}
SPQuery query = new SPQuery();
query.ViewAttributes = "Scope=\"Recursive\"";
SPListItemCollection allItems = currList.GetItems(query);
foreach (SPListItem item in allItems)
{
cbItemList.Items.Add(item.ID);
}
To Get All Items From All Sub-Folders Recursively With Sub-Folders
you can use next without considering the throttling (which you should. I will address this later).
SPQuery query = new SPQuery();All Items from a Larget List
query.ViewAttributes = "Scope=\"RecursiveAll\"";
SPListItemCollection allItems = currList.GetItems(query);
foreach (SPListItem item in allItems)
{
cbItemList.Items.Add(item.ID);
}
To get all items from a large list, you have to use "query.ListItemCollectionPosition" in a while loop to get a smaller number of items at a time.
A candidate code may look like this
// Get all items in the list including all items under sub folders but not include folder itemsSPListItemCollectionPosition currPosition = null;
SPListItemCollectionPosition nextPosition = null;
do
{
string camlQuery = "<Where><Geq><FieldRef Name=\"Modified\" /><Value IncludeTimeValue=\"TRUE\" Type=\"DateTime\">" + SPUtility.CreateISO8601DateTimeFromSystemDateTime(currentDate) + "</Value></Geq></Where>";
SPListItemCollection allItems = SPHelper.GetItemsFromLargeList(sourceList, camlQuery, 200, currPosition, out nextPosition, "City", "State", "Country");
// List.Items get items from all sub folders recursively but it doesn't contain folder item
foreach (SPListItem item in allItems)
{
... ...
}
currPosition = nextPosition;
// break the pagination while loop if the current position is nullif (currPosition == null)
break;
} while (true);
public static SPListItemCollection GetItemsFromLargeList(SPList largeList, string camlQuery, uint pageSize, SPListItemCollectionPosition currentPosition, out SPListItemCollectionPosition nextPosition, params string[] fieldNames)
{
SPQuery query = new SPQuery();
// Include only the fields you will use for better performance.
if (fieldNames != null)
{
foreach (string fieldName in fieldNames)
{
query.ViewFields += "<FieldRef Name=\"" + fieldName + "\"/>";
}
}
// Only select the top pageSize items.
query.RowLimit = pageSize;
// Get all non-folder items recursively from sub folders. To get folder items also, use RecursiveAll
query.ViewAttributes = "Scope=\"Recursive\"";
// To make the query order by ID and stop scanning the table, specify the OrderBy override attribute.
query.Query = camlQuery + "<OrderBy Override=\"TRUE\"><FieldRef Name=\"ID\"/></OrderBy>";
// Set current pagination position
query.ListItemCollectionPosition = currentPosition;
SPListItemCollection itemList = largeList.GetItems(query);
// Set next pagination position
nextPosition = itemList.ListItemCollectionPosition;
return itemList;
Can i have your code sample please
ReplyDelete