.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> SharePoint >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Using SPListItemCollectionPosition for Faster Results in SharePoint 2010

Posted By:Jean Paul       Posted Date: March 13, 2013    Points: 200    Category: SharePoint    URL: http://www.dotnetspark.com  

In this article we can explore the class SPListItemCollectionPosition and its advantage. This is a server object model type and should be used along with the modules executed on the server.
 


In this article we can explore the class SPListItemCollectionPosition and its advantage.  This is a server object model type and should be used along with the modules executed on the server.

What is SPListItemCollectionPosition?


This class enables Pagination for Lists inside SharePoint.  It stores the state of a page and thus helps in fetching the next page in the dataset.

The class can be used along with SPQuery object.

What are the advantages of SPListItemCollectionPosition?


It provides the following advantages:
1.  Reduced network traffic
2.  Reduced memory
3.  Faster result delivery

Going Practical


Now we can try ourselves the usage of SPListItemCollectionPosition.  Here we are going to create 1000 list items and fetching 10 at a time.  The web part associated will be containing page navigation buttons.

Step 1: Create a Visual WebPart solution

Create a new Visual WebPart solution and give it a meaningful name.





Step 2: Place the following controls

Place the following controls for the WebPart.


Step 3: Create the Data

Now we needed to create a list with 1000 items.  The following code does the purpose.  Place the code inside the Created Data button click event.

protected void CreateDataButton_Click(object sender, EventArgs e)
 {
   using (SPWeb web = SPContext.Current.Web)
   {
   string listName = "Contacts 1000";
   SPList list = null;

   // Create Lists
   try
   {
   list = web.Lists[listName];
   }
   catch
   {
   web.Lists.Add(listName, "A Contact List of 1000 items", SPListTemplateType.Contacts);
   list = web.Lists[listName];

   // Create Data
   for (int i = 1; i <= 1000; i++)
   {
   SPListItem item = list.Items.Add();
   item["First Name"] = "First Name " + i.ToString();
   item["Last Name"] = "Last Name " + i.ToString();

   item.Update();
   }
   }
   }
 }


Now we can try creating the data by executing the above code.  The steps are:
1.  Build the solution
2.  Deploy the solution
3.  Insert WebPart into Page
4.  Click the Create Data button
You can see that inside the Lists link a new list named Contacts 1000 is created with the following data.


Step 4: Show the Data

Now we are ready to show the first 10 list items.  Here we are showing only 10 items per page and saving the Page information in the View State of User Control.

protected void ShowDataButton_Click(object sender, EventArgs e)
 {
   SPQuery query = new SPQuery();
   query.RowLimit = 10;
   query.ViewFields = "<FieldRef Name=\"Title\" />" +  /* Title is LastName column */
   "<FieldRef Name=\"FirstName\" Nullable=\"TRUE\" Type=\"Text\"/>";
   string listName = "Contacts 1000";
   SPList list = SPContext.Current.Web.Lists[listName];
   
  SPListItemCollection collection = list.GetItems(query);
   
  PagingInfo.SavePageInfo(ViewState, collection.ListItemCollectionPosition.PagingInfo);
   
  GridView1.DataSource = collection.GetDataTable();
   GridView1.DataBind();
 }


Note: Here we are using the SPListItemCollection object to memorize the items fetched.  To persist this information between page requests we are saving it to the ViewState using PagingInfo class.

You can execute the code and see the results as below:


Note: In this case the ViewState contains following data: Paged=TRUE&p_ID=10. It stores the last ID of the item displayed.  This ID is required for processing the next button.

Step 5: Next Page Functionality

Following is the code for Next Page button functionality.

protected void NextPageButton_Click(object sender, EventArgs e)
 {
   SPQuery query = new SPQuery();
   query.RowLimit = 5;
   query.ViewFields = "<FieldRef Name=\"Title\" />" +  /* Title is LastName column */
    "<FieldRef Name=\"FirstName\" Nullable=\"TRUE\" Type=\"Text\"/>";
   string listName = "Contacts 1000";
   SPList list = SPContext.Current.Web.Lists[listName];

   /* New */
   query.ListItemCollectionPosition = PagingInfo.GetNextPagePosition(ViewState);

   SPListItemCollection collection = list.GetItems(query);

   PagingInfo.SavePageInfo(ViewState, collection.ListItemCollectionPosition.PagingInfo);

   GridView1.DataSource = collection.GetDataTable();
   GridView1.DataBind();
 }



The code takes the Page Position from the View State using the PagingInfo class.  You can see the following results on executing and clicking the Next button.


So this concludes our experiment with the SPListItemCollectionPosition class.

About the Utility Class


Following is the body of PagingInfo class.  It is a static class basically doing the SavePageInfo() and GetNextPagePosition() functionalities by saving the Page Info string to View State and getting back.

public static class PagingInfo
 {
   public static void SavePageInfo(StateBag viewState, string pagingInfo)
   {
   viewState["PagingInfo"] = pagingInfo;
   }

   public static SPListItemCollectionPosition GetNextPagePosition(StateBag viewState)
   {
   string pagingInfo = string.Empty;

   if (viewState["PagingInfo"] != null)
   pagingInfo = viewState["PagingInfo"].ToString();

   return new SPListItemCollectionPosition(pagingInfo);
   }
 }


So this concludes the usage of SPListItemCollectionPosition class.

About the Previous Page Functionality


As the SPListItemCollectionPosition is meant for providing paginated results for the next page by storing the ID value of the last item in the page.  So to perform the functionality of Previous page we need to work more on getting the ID of the first item in the page.  I have added some links that should give an idea on enabling the Previous functionality.


References


  MSDN Information 
  Previous Page Functionality
  Paging with SPListItemCollectionPosition

Summary


In this article we have explored the advantage and use of SPListItemCollectionPosition.  In real world scenarios this class provides faster results with reduced network traffic involved.  The attachment contains the source code we discussed.  You can download and install it to your SharePoint site for testing.

You can also download sample probject


 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend