寻呼SharePoint列表项是不是直线前进的任务。你必须做一些水管,使其工作。特别,如果你有排序和筛选查询你想要得到您的项目。这是一个试验,我认为,将会使任务更容易:
SPPagedListItemsRetriever是,我们需要实例化,我们需要使用指定的检索SPList类,SPQuery,我们将执行。SPPagedListItemsRetriever pagedItemsRetriever = new SPPagedListItemsRetriever(list, query);
然后,我们有2个公共方法:
{C}
在实施技术的缺点是,它总是从一开始的项目!
所以,如果你要求得到项目由200项和页面大小10起
它会得到的第一个199项目,并把它们扔掉,然后开始获取所需的项目,从200到210。
下面是完整的代码后:
的{S0的} /// <summary>
/// Retrieves paginated items of SPList with the specified SPQuery.
/// </summary>
public class SPPagedListItemsRetriever
{
private SPQuery _query;
private SPList _list;
private const int MaxRowLimit = 2000;
private static SPListItemCollectionPosition _emptySPListItemCollectionPosition = new SPListItemCollectionPosition(string.Empty);
/// <summary>
/// Constructs a new instance of SPPagedListItemsRetriever
/// </summary>
/// <param name="list">The list to get the items from</param>
/// <param name="query">The query by which the items should be retrieved</param>
public SPPagedListItemsRetriever(SPList list, SPQuery query)
{
_list = list;
_query = query;
}
/// <summary>
/// Get the items of the list with the specified query begining from a specified startIndex and with maxRowsCount (PageSize)
/// </summary>
/// <param name="startIndex"></param>
/// <param name="maxRowsCount"></param>
/// <returns></returns>
public SPListItem[] GetItems(int? startIndex, int? maxRowsCount)
{
SPListItemCollectionPosition listItemCollectionPosition = null;
uint actualStartIndex = startIndex.HasValue ? (uint)startIndex.Value : 0;
//If we need items beginning from a specific index (greater that 0, the first one)
//Create a dummy query to begin getting the items from the first one (0) till we reach the specified startIndex
if (actualStartIndex > 0)
{
SPQuery dummyQuery = new SPQuery();
//Change the ViewFields returned from this dummy query to minimal, actually we dont need these items so selelct the ID only to minimize the view fields
dummyQuery.ViewFields = "<FieldRef Name='ID' />";
dummyQuery.Query = _query.Query;
if (null != _query.Folder)
dummyQuery.Folder = _query.Folder;
int gotDummyItems = 0;
do
{
//Minimize the number of items not to exceed the recommended 2000 MaxRowLimit for SPQuery
dummyQuery.RowLimit = Math.Min((uint)(actualStartIndex - gotDummyItems), MaxRowLimit);
if (null == listItemCollectionPosition)
listItemCollectionPosition = _emptySPListItemCollectionPosition;
dummyQuery.ListItemCollectionPosition = listItemCollectionPosition;
SPListItemCollection items = _list.GetItems(dummyQuery);
gotDummyItems += items.Count;
listItemCollectionPosition = items.ListItemCollectionPosition;
}
while (gotDummyItems < actualStartIndex && listItemCollectionPosition != null);
}
//Now we will get the actual items we need
SPQuery query = new SPQuery();
query.Query = _query.Query;
if (null != _query.Folder)
query.Folder = _query.Folder;
query.ViewFields = _query.ViewFields;
List<SPListItem> returnedItemsList = new List<SPListItem>();
uint actualMaxRowCount = maxRowsCount.HasValue ? (uint)maxRowsCount.Value : (uint)_list.ItemCount;
do
{
//Minimize the number of items not to exceed the recommended 2000 MaxRowLimit for SPQuery
query.RowLimit = Math.Min(actualMaxRowCount, MaxRowLimit);
if (null == listItemCollectionPosition)
listItemCollectionPosition = _emptySPListItemCollectionPosition;
query.ListItemCollectionPosition = listItemCollectionPosition;
SPListItemCollection listItems = _list.GetItems(query);
returnedItemsList.AddRange(listItems.Cast<SPListItem>().Select(i=>i));
listItemCollectionPosition = listItems.ListItemCollectionPosition;
}
while (returnedItemsList.Count < actualMaxRowCount && listItemCollectionPosition != null);
return returnedItemsList.ToArray();
}
/// <summary>
/// Gets the total items count using the specified query
/// </summary>
/// <returns></returns>
public int GetTotalItemsCount()
{
SPQuery query = new SPQuery();
//Change the ViewFields returned from this dummy query to minimal, actually we dont need these items so selelct the ID only to minimize the view fields
query.ViewFields = "<FieldRef Name='ID' />";
query.Query = _query.Query;
SPFolder folder = _query.Folder;
if (null != folder)
query.Folder = folder;
return _list.GetItems(query).Count;
}
}