Sharepoint List Service – GetListItems and Pagination


Ran into this problem and searched high and low for a solution but never found another post detailing the solution.


The issue is that, according to the documentation for GetListItem, located here:


http://msdn.microsoft.com/en-us/library/lists.lists.getlistitems.aspx


Paging should be  done like this:


The GetListItems method supports server-side paging. The XML data returned by this method includes a ListItemCollectionPositionNext attribute inside the rs:Data element that contains the information to support paging. This string contains data for the fields in the sort and for other items needed for paging. You should consider this string internal and not to be modified; modifying it can produce unexpected results. The following example shows the form of this return value when paging is supported.










Xml
<rs:Data ListItemCollectionPositionNext=”
Paged=TRUE&p_ID=100&View=
%7bC68F4A6A%2d9AFD%2d406C%2dB624%2d2CF8D729901E%7d&PageFirstRow=
101″ Count=1000 >
<z:row ows_FirstName=”Nancy” ows_LastName=”Name” ….. />

</rs:Data>

To get the next page of data, the queryOption parameter is used, as shown in the following example.










Xml
<QueryOptions>
<Paging ListItemCollectionPositionNext=”
Paged=TRUE&p_ID=100&View=
%7bC68F4A6A%2d9AFD%2d406C%2dB624%2d2CF8D729901E%7d&PageFirstRow=
101″ />
</QueryOptions>
 

The issue with the above documentation is that when you try to load the above into an XmlElement, then it will fail because the attribute has invalid characters in it.  You can try escaping those characters, however then Sharepoint will not recognize it as a valid pagination token and you wont get proper pagination.

 

 

The trick is to build the XML string with that information blank like this:


 


<QueryOptions>


  <Paging ListItemCollectionPositionNext=”” />


</QueryOptions>


 


Then load it into the XmlElement using the InnerXML, as before.


 


optionsQuery.InnerXml = queryOptions;


 


The go directly to the attributes innertext and set the value:


optionsQuery.ChildNodes[0].Attributes[“ListItemCollectionPositionNext”].InnerText = “Paged=TRUE&p_ID=100&View=
    %7bC68F4A6A%2d9AFD%2d406C%2dB624%2d2CF8D729901E%7d&PageFirstRow=101″;


 


 

Comments (9)

  1. kevinidzi says:

    These kinds of little things are why I made a reusable component you can use instead of hardwiring the XML yourself at:

    http://www.codeplex.com/SQLSrvIntegrationSrv

    In the SharePoint List Source and Destination Sample, there is a subcomponent used (GACed DLL) called SharePointUtility which wraps the getting of list data, setting of the data, getting column information and even uploading/removing files.  That subcomponent can be very useful and handles things like paging, or reducing the # of columns in a .net 3.5 Linq friendly syntax.  

    – Kevin I

  2. yangxind says:

    All you need to do is XML encode the "&" as

    queryOptions.InnerXml = "<Paging ListItemCollectionPositionNext="Paged=TRUE&amp;p_Modified=20081021%2022%3a45%3a51&amp;p_ID=9" />";

  3. yangxind says:

    You shouldn’t need to build this pagination query option string manually. You just need to get it from the XmlNode returned by GetListItems and attach it to the queryOptions’s innerXml. Please see details at http://ethan-deng.spaces.live.com/blog/cns!1F93FA6E149393C7!118.entry

  4. The XmlNode that is returned from GetListItems has a pointer to the next page ONLY.

    At one point we were using GetList and GetListItems to create and pupulate an ADO.Net DataTable with the entire List. Once we have the DataTable, we bind it to a GridView control on and ASP.Net web page. We gave the client the ability to Filter, Sort and Update the data. This is a much nicer interface than anything SharePoint has.

    However, when the List became too large, GetListItems would throw the Soap "System.OutOfMemoryException".

    We had one of our subs build us a C# Class Library that is basically a wrapper around GetList, GetListItems and UpdateListItems.

    This Wrapper works in the following manner: When the class is instantiated, the ListURL, ListName, RowsPerPage, Query and ViewFields need to be set. GetSPList will return the first page as an Ado.Net DataTable. It also has the PageNext, PagePrevious and GotoPage(X) methods that return an Ado.Net DataTable. Other read only properties include CurrentPage and TotalPages.

    If ViewFields, Query or RowsPerPage are reset, it will recalculate TotalPages. GetListItems is another read only property that holds the GetListItems XmlNode. This has worked perfectly for us and solved our problems.

    They told us this assembly was written in C# and was unit tested using WSS 3.0, MOSS 2007, IE 8.0 and Windows Server 2003 32bit.

    We purchased only the Assembly. Not the code. If you want, I can put you in touch with these people.

  5. Venkatesh says:

    Yeehaaa..

    @Yangxin Deng : Thanks Dude. This is what am searching..

                       nextPagePointer = "<Paging ListItemCollectionPositionNext='" + listPaginationPointer + "' />";

                       nextPagePointer = nextPagePointer.Replace("&", "&");

                       ndQueryOptions.InnerXml = nextPagePointer;

  6. Ram says:

    Excellent Post. It really solved my issue

  7. Sobu says:

    hi guys, requesting your quidance…. I have a process that runs on each document on document libraries in multiple site collections. This uses the MOSS web services to fetch data and then run some process on it.. I need to run the process for 100 docs at a time on all document libraries on all Site Collections.. one of the document library has about 1500 docs! The error that I receive if I run on all documents at once is "Error in xml document (1056, 1256)"

  8. Taylorsammy says:

          The Ecommerce solutions include a website or a web based system, an impressive shopping cart, payment gateway integration, secure and scalable hosting and marketing knowledge.

    <a href="grouponclone.ideazglobal.com/">groupon script</a>

  9. Niteen Badgujar says:

    have a look at my blog for other information on Pagination,

    http://www.niteenbadgujar.com/…/sharepoint-client-object-model-caml.html

    hope that will help others   🙂