Ethan Deng's SharePoint Blog

About Me
SharePoint
Blog
List Pagination 1
List Pagination 2
Customize Home Page
Excel Services Issue
Error with mngfield Page
User Defined Function
Enable Incoming Email
Install SP2 On R2
SharePoint Content Migrat
SharePoint2010 Installati
Troubleshoot Search
Dash Sign And Search
Search Pattern Issue
Create Dropdown Menu
Email Enabled Custom List
Email Enabled Issue List
SharePoint Folder
Cost Effective Small Farm
SharePoint Throttling
SystemUpdate And Alerts
A Parallel Approach

SharePoint List Pagination using Object Model

 

Comparing doing list pagination with Sharepoint Web Services, pagination using Sharepoint Object Model is relatively easy. Please see my article about pagination with Sharepoint Web Services. “Sharepoint List Pagination using Web Service API

All you need to do is to set the RowList preperties and ListItemCollectionPosition properties of the Query object you are going to use in the SPList GetItems() method. Please reference MSDN article "SPQuery.RowLimit Property (Microsoft.SharePoint)"

Let's start with getting list items without pagination. Normally to get list items from a Sharepoint list, you will write next code.

        private void btListGetItems_Click(object sender, EventArgs e)

        {

            rtbResult.Clear();

            using (SPSite site = new SPSite("http://machine/sites/AllSharepointNews/"))

            {

                using (SPWeb web = site.AllWebs["/sites/AllSharepointNews"])

                {

                    SPList list = web.Lists["ITNews"];

                    SPQuery query = new SPQuery();

                    query.Query = "<OrderBy><FieldRef Name='Created' Ascending='FALSE' /></OrderBy>";

                    int index = 0;

 

                    SPListItemCollection itemList = list.GetItems(query);

 

                    foreach (SPListItem item in itemList)

                    {

                        index++;

                        rtbResult.Text += index.ToString() + " " + item["Title"] as string + Environment.NewLine;

                    }

 

                }

            }

        }

A sample of result from running the above code.

1 Pirates of the Amazon hits the rocks - CNET News

2 Amnesia victim &#39;HM,&#39; who shed light on brain, dies - The Associated Press

3 Nokia 2605 debuts on Verizon - CNET News

4 Flickr Adds Video Playback to Mobile Site - PC Magazine

5 AT&amp;T wants a single smartphone platform on its network; I wanna fly - IntoMobile

6 IBM Launches Microsoft-Free Linux PCs - InformationWeek

7 Vista SP2 Beta Available For Public Download - InformationWeek

8 Intel Hopes to Bring Free Energy to Mobile Devices - PC World

9 Obama&#39;s Zune story crashes news site - United Press International

10 Amazon&#39;s Kindle: a great gift for Washington&#39;s Birthday? - CNET News

11 Storm Fans Lash at Critics - PC World

12 Verizon expected to release firmware update for BlackBerry Storm - CNET News

13 Apple to Sell IPhones in Wal-Mart Stores This Month (Update1) - Bloomberg

14 Google&#39;s Chrome Team Mulls Local File Restrictions - InformationWeek

15 Facebook, Google Vie for Connect Services - PC World

16 Endeavour delays return trip to Fla. until Tuesday - San Jose Mercury News

17 Apple hits 300 million App Store downloads - Afterdawn.com

18 Facebook Worm Comes From Infected Friends - InformationWeek

19 NASA delays Mars rover mission two years - CNET News

20 Intel develops fast, cheap optical links on silicon - CNET News

 

 

To add pagination functionality to this method, you need to

1.Set the RowList of the Query object.

2.Add another while loop to loop through each page

3.When GetItems is called, retrieve the SPListItemCollectionPosition  object returned by the GetItems method as property of SPListItemCollection  object. And set it to the same property of the Query object.

       private void btListGetItemsInPages_Click(object sender, EventArgs e)

        {

            rtbResult.Clear();

            using (SPSite site = new SPSite("http://machine/sites/AllSharepointNews/"))

            {

                using (SPWeb web = site.AllWebs["/sites/AllSharepointNews"])

                {

                    SPList list = web.Lists["ITNews"];

                    SPQuery query = new SPQuery();

                    query.Query = "<OrderBy><FieldRef Name='Created' Ascending='FALSE' /></OrderBy>";

 

                    query.RowLimit = 3;

                    int index = 0;

                    int pageCount = 0;

                    do

                    {

                        pageCount++;

                        rtbResult.Text += "------------- Page " + pageCount + "------------------" + Environment.NewLine;

                        SPListItemCollection itemList = list.GetItems(query);

 

                        foreach (SPListItem item in itemList)

                        {

                            index++;

                            rtbResult.Text += index.ToString() + " " + item["Title"] as string + Environment.NewLine;

                        }

 

                        query.ListItemCollectionPosition = itemList.ListItemCollectionPosition;

 

                        if (query.ListItemCollectionPosition != null)

                        {

                            rtbResult.Text += "PageInfo: " + itemList.ListItemCollectionPosition.PagingInfo + Environment.NewLine;

                        }

                    }

                    while (query.ListItemCollectionPosition != null);

                }

            }

        }

Also notice that you have to reuse the same query object for all the subsequent GetItems calls. Creating new Query object will not work.

A sample out is

------------- Page 1------------------

1 Pirates of the Amazon hits the rocks - CNET News

2 Amnesia victim &#39;HM,&#39; who shed light on brain, dies - The Associated Press

3 Nokia 2605 debuts on Verizon - CNET News

PageInfo: Paged=TRUE&p_Created=20081208%2006%3a24%3a38&p_ID=20

------------- Page 2------------------

4 Flickr Adds Video Playback to Mobile Site - PC Magazine

5 AT&amp;T wants a single smartphone platform on its network; I wanna fly - IntoMobile

6 IBM Launches Microsoft-Free Linux PCs - InformationWeek

PageInfo: Paged=TRUE&p_Created=20081208%2006%3a24%3a37&p_ID=16

------------- Page 3------------------

7 Vista SP2 Beta Available For Public Download - InformationWeek

8 Intel Hopes to Bring Free Energy to Mobile Devices - PC World

9 Obama&#39;s Zune story crashes news site - United Press International

PageInfo: Paged=TRUE&p_Created=20081208%2006%3a24%3a36&p_ID=10

------------- Page 4------------------

10 Amazon&#39;s Kindle: a great gift for Washington&#39;s Birthday? - CNET News

11 Storm Fans Lash at Critics - PC World

12 Verizon expected to release firmware update for BlackBerry Storm - CNET News

PageInfo: Paged=TRUE&p_Created=20081208%2006%3a24%3a36&p_ID=13

------------- Page 5------------------

13 Apple to Sell IPhones in Wal-Mart Stores This Month (Update1) - Bloomberg

14 Google&#39;s Chrome Team Mulls Local File Restrictions - InformationWeek

15 Facebook, Google Vie for Connect Services - PC World

PageInfo: Paged=TRUE&p_Created=20081208%2006%3a24%3a35&p_ID=8

------------- Page 6------------------

16 Endeavour delays return trip to Fla. until Tuesday - San Jose Mercury News

17 Apple hits 300 million App Store downloads - Afterdawn.com

18 Facebook Worm Comes From Infected Friends - InformationWeek

PageInfo: Paged=TRUE&p_Created=20081208%2006%3a24%3a34&p_ID=5

------------- Page 7------------------

19 NASA delays Mars rover mission two years - CNET News

20 Intel develops fast, cheap optical links on silicon - CNET News

 

 

Notice I also print out the “PagingInfo” property using itemList.ListItemCollectionPosition.PagingInfo. You can see that this string returned in this property actually contains the next page’s information for the subsequent query to get the next page.

Paged=TRUE&p_Created=20081208%2006%3a24%3a38&p_ID=20

It is also worth to notice that the Shareponit web’s list view pagination is implemented in a similar way. If you turn on the list view’s pagination, the url to each paged list looks like

http://machine/sites/MyITNews/Lists/ITNews/AllItems.aspx?Paged=TRUE&p_ID=3&View=%7b639D1491%2d1488%2d430C%2dAB7C%2d69F6AE07AB48%7d&PageFirstRow=4