- Reading all items in a large list
SharePoint 2010, SPQuery , . " , , ". , Content Iterator.
ContentIterator , http://msdn.microsoft.com/en-us/library/ee560760%28v=office.14%29.aspx
ContentIterator, Microsoft.Office.Server.dll 14/ISAPI/ Microsoft.Office.Server.Utilities.
:
, .
, List View Threshold, . SPQuery .
.
:
SPQuery.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.Office.Server.Utilities;
namespace ContentIteratorListItemCollBatch
{
class Sample
{
static int NumberOfBatch = 0, NumberOfItemsRead = 0, NumberOfException = 0;
static void Main(string[] args)
{
using (SPSite site = new SPSite("your site url"))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.GetList("Lists/LargeList/AllItems.aspx");
ContentIterator ci = new ContentIterator("Reading All Items");
SPQuery qry = new SPQuery();
qry.QueryThrottleMode = SPQueryThrottleOption.Strict;
qry.RowLimit = 2000;
qry.Query = qry.Query + ContentIterator.ItemEnumerationOrderByID;
ci.ProcessListItems(list, qry, ProcessItemColl, ProcessErrorColl);
Console.WriteLine("\nBatch count: " + NumberOfBatch + "\n\nTotal number of items read: " + NumberOfItemsRead);
Console.ReadLine();
}
}
}
static public bool ProcessErrorColl(SPListItemCollection itemColl, Exception e)
{
NumberOfException++;
return true;
}
static public void ProcessItemColl(SPListItemCollection itemColl)
{
Console.WriteLine("Number of Items Read: " + itemColl.Count);
NumberOfBatch++;
NumberOfItemsRead += itemColl.Count;
}
}
}
25 000 . , 25 000 2000 .
Number of Items Read: 2000
Number of Items Read: 2000
Number of Items Read: 2000
Number of Items Read: 2000
Number of Items Read: 2000
Number of Items Read: 2000
Number of Items Read: 2000
Number of Items Read: 2000
Number of Items Read: 2000
Number of Items Read: 2000
Number of Items Read: 2000
Number of Items Read: 2000
Number of Items Read: 1000
Batch count: 13
Total number of items read: 25000
.
.
ContentIterator.ItemEnumerationOrderByNVPField.
Title . , .
//
System;
System.Collections.Generic;
System.Linq;
System.Text;
Microsoft.SharePoint;
Microsoft.Office.Server.Utilities;
namespace ContentIteratorListItemCollBatch
{
class Sample
{
static int NumberOfBatch = 0, NumberOfItemsRead = 0, NumberOfException = 0;
static void Main(string[] args)
{
using (SPSite site = new SPSite("your site url"))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.GetList("Lists/LargeList/AllItems.aspx");
ContentIterator ci = new ContentIterator("Reading All Items");
SPQuery qry = new SPQuery();
qry.QueryThrottleMode = SPQueryThrottleOption.Strict;
qry.RowLimit = 2000;
qry.Query = @"<Where><Contains><FieldRef Name='Title' /><Value Type='Text'>9</Value></Contains></Where>";
qry.Query = qry.Query + ContentIterator.ItemEnumerationOrderByNVPField;
ci.ProcessListItems(list, qry, ProcessItemColl, ProcessErrorColl);
Console.WriteLine("\nBatch count: " + NumberOfBatch + "\n\nTotal number of items read: " + NumberOfItemsRead);
Console.ReadLine();
}
}
}
static public bool ProcessErrorColl(SPListItemCollection itemColl, Exception e)
{
NumberOfException++;
return true;
}
static public void ProcessItemColl(SPListItemCollection itemColl)
{
Console.WriteLine("Number of Items Read: " + itemColl.Count);
NumberOfBatch++;
NumberOfItemsRead += itemColl.Count;
}
}
}
SPQuery, , , . ContentIterator .
Output
Number of Items Read: 2000
Number of Items Read: 2000
Number of Items Read: 2000
Number of Items Read: 2000
Number of Items Read: 233
Batch count: 5
Total number of items read: 8233