分页 SharePoint 列表项





5.00/5 (1投票)
支持 SharePoint 列表项分页的实现
对 SharePoint 列表项进行分页不是一件直接的任务。 你需要进行一些准备工作才能使其正常运行,尤其是在你想要对查询应用排序和筛选的情况下。
这是一个尝试,我认为它会使这项任务更容易。
SPPagedListItemsRetriever
是一个需要通过指定要使用的 SPList
和要执行的 SPQuery
来实例化的类。
SPPagedListItemsRetriever pagedItemsRetriever = new SPPagedListItemsRetriever(list, query);
然后,我们有两个 public
方法
public SPListItem[] GetItems(int? startIndex, int? maxRowsCount);
public int GetTotalItemsCount();
这种实现中使用的技术的缺点是,它总是从头开始获取项目!
因此,如果你要求从第 200 个项目开始,页面大小为 10,它将获取前 199 个项目并丢弃它们,然后从第 200 个项目到第 210 个项目开始获取所需的项目。
这是代码的完整帖子
/// <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 name="query" />The query by which the items should be retrieved
public SPPagedListItemsRetriever(SPList list, SPQuery query)
{
_list = list;
_query = query;
}
/// <summary>
/// Get the items of the list with the specified query
/// beginning from a specified startIndex and with maxRowsCount (PageSize)
/// </summary>
/// <param name="startIndex" />
/// <param name="maxRowsCount" />
/// <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 select 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;
}
}