I am trying to page the requests of Data from SPLists. I am using the SPQuery's ViewFields property to only get the fields required. Also I am trying to use the SPQuery's ListItemCollectionPosition property (Paged=TRUE&p_ID={0}) and RowLimit to do the same. I am not able to get it working for all scenarios. They are listed below:
1. If an item in the list is deleted then i will not be able to set the p_ID correctly.
2. If the SPQuery has an orderby clause then setting the p_ID is tough.
3. Moreover, if the caml query is trying to filter listitems based on conditions, then i dont know how to set the ListItemCollectionPosition property.
I noticed that, the default list view is able to page according to the number of items set as the RowLimit and irrespective of items deleted in between, it is able to paginate properly. Please help.
private const string listItemCollectionPostion = "Paged=TRUE&p_ID={0}";
public class PagingInfo
{
// corresponding properties exist
private int elementsPerPage;
private int pageRequested;
private string orderByField;
private bool isAscending;
}
-- Method code below --
// Set the listitemcollection position to the page requested * elements per page
int itemCollectionPosition = 0;
itemCollectionPosition = (pagingInfo.PageRequested * pagingInfo.ElementsPerPage);
query.ListItemCollectionPosition = new SPListItemCollectionPosition(string.Format(listItemCollectionPostion, itemCollectionPosition));
// Set the rowlimit to the number of items requested
query.RowLimit = Convert.ToUInt32(pagingInfo.ElementsPerPage);
totalPages = Convert.ToInt32(Math.Ceiling( web.Lists[listName].ItemCount / (decimal)pagingInfo.ElementsPerPage));
If anybody could post a code sample which does the pagination for SharePoint lists. That would be great.
Thanks,
Puneet.H