我想要查询的列表中有大约10000多行(Listitems)。
我想迭代一个timerJob中的每个项目 - 但我不能一次完成所有这些项目 因为:对象模型覆盖 - NO,ListView阈值 - 1000 - 在FARM级别,我不能改变它。
对我来说,迭代所有10000+(如批处理)的方法是什么?
答案 0 :(得分:5)
您应该使用ContentIterator。这将允许您迭代非常大的列表的内容,而不会触发SPQueryThrottledException
。
例如:
SPList list = SPContext.Current.Web.Lists["MyList"];
// Build query using an iterator-defined WHERE clause
string query = string.Format("<Where><Eq><FieldRef Name='MyFieldName'/><Value Type='Text'>MyFieldValue</Value></Eq></Where>{0}", ContentIterator.ItemEnumerationOrderByNVPField);
// Instantiate iterator and use it to process the list
ContentIterator iterator = new ContentIterator();
iterator.ProcessListItems(list, query, ProcessListItem, ProcessListItemError);
然后你就这样定义了ProcessListItem
和ProcessListItemError
:
static void ProcessListItem(SPListItem item) {
Console.WriteLine("ProcessListItem: Name {0}", item.Title);
}
static bool ProcessListItemError(SPListItem item, Exception e) {
Console.WriteLine("ERROR: message {0}", e.Message);
return true;
}
我还建议您查看Microsoft的Best Practices with SharePoint Server articles,特别是“Writing Efficient Code In SharePoint Server”,它会进一步讨论正确编写查询。
答案 1 :(得分:0)
内容迭代器是您可以使用的最佳技术之一。不要使用使用索引字段的order by子句 ContentIterator.ItemEnumerationOrderByNVPField ,否则内容迭代器在处理大项时可能会抛出异常。
本文最好地介绍了内容迭代器以及其他技术:
http://extreme-sharepoint.com/2012/07/17/data-access-via-caml-queries/