在 this文章中,微软解释了为什么有5000项限制.但是我何时可以访问超过5000件物品?
例:
我有一个包含50 000个项目的列表,我执行一个行限制为50的caml查询.Sharepoint是锁定数据库还是只有50行? sharepoint如何知道是否只有一些行,或整个数据库应该被锁定?或者取决于它本身?
锁是否会影响整个服务器场或仅影响当前列表,因为sharepoint没有为每个列表创建一个自己的表?
最佳答案 >阅读大型清单中的所有项目
在SharePoint 2010中,当您在大型列表上执行SPQuery时,会出现异常“禁止尝试的操作,因为它超出了管理员强制执行的列表视图阈值”.为避免此异常并按批次读取列表项,我们可以使用Content Iterator.
ContentIterator提供了很多方法,我们在这里讨论大约http://msdn.microsoft.com/en-us/library/ee560760%28v=office.14%29.aspx
要使用ContentIterator,请在14 / ISAPI /中包含Microsoft.Office.Server.dll,并包含名称空间Microsoft.Office.Server.Utilities.
优点:
获取批处理列表项,减少负载.
如果索引列条件返回的值多于列表视图阈值,则按批处理.正常SPQuery在这种情况下失败.
我们可以随时停止批处理.
坏处:
您不能在SPQuery条件中包含非索引列.
//Run as console application
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"); //your list url
ContentIterator ci = new ContentIterator("Reading All Items");
SPQuery qry = new SPQuery();
qry.QueryThrottleMode = SPQueryThrottleOption.Strict; //Ensuring that all users come under List View Threshold. (Including farm admins / box administrators).
qry.RowLimit = 2000; //Number of Items read in a batch. But it should be less than List View Threshold.
qry.Query = qry.Query + ContentIterator.ItemEnumerationOrderByID; //Not Required, Include for faster output.
//Don't use ContentIterator.ItemEnumerationOrderByNVPField, it gets into infinite loop.
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)
{
// process the error
NumberOfException++;
return true;
}
static public void ProcessItemColl(SPListItemCollection itemColl)
{
//Work on the ListItem Collection object with your own condition
//foreach (SPListItem item in itemColl)
//{
//}
Console.WriteLine("Number of Items Read: " + itemColl.Count);
NumberOfBatch++;
NumberOfItemsRead += itemColl.Count;
}
}
}
我的大清单包含25,000个项目.你可以从输出中看到,它按批次的2000个项目读取了25,000个项目.
产量
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
>读取带有条件的大型列表中的项目
确保满足以下条件.
在where where条件下只允许使用Indexed列.
您应该包含ContentIterator.ItemEnumerationOrderByNVPField.
在下面的代码中,Title是一个索引列.创建自定义列表后,标题将作为索引列.
//作为控制台应用程序运行
使用系统;
使用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"); //your list url
ContentIterator ci = new ContentIterator("Reading All Items");
SPQuery qry = new SPQuery();
qry.QueryThrottleMode = SPQueryThrottleOption.Strict; //Ensuring that all users come under List View Threshold. (Including farm admins / box administrators).
qry.RowLimit = 2000; //Number of Items read in a batch. But it should be less than List View Threshold.
qry.Query = @"<Where><Contains><FieldRef Name='Title' /><Value Type='Text'>9</Value></Contains></Where>";
qry.Query = qry.Query + ContentIterator.ItemEnumerationOrderByNVPField;
//Have to include this line.
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)
{
// process the error
NumberOfException++;
return true;
}
static public void ProcessItemColl(SPListItemCollection itemColl)
{
//Work on the ListItem Collection object with your own condition
//foreach (SPListItem item in 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