ASP.NET, Uncategorized

Sharepoint Server Side Pagination

  Sharepoint Server Side Pagination

Description

In this blog, I am going to explain how to apply pagination on the Sharepoint list data. As we know, a Sharepoint list can contain many number of items, so fetching those n number of items and rendering those in one page consumes a lot of page loading time. To avoid this, we can use the concept of pagination throughSharepoint. In this blog, I will explain how to achieve it through server side.

We will fetch the data from Sharepoint list using SPQuery and SPListItemCollectionPosition.

SPListItemCollectionPosition

The key to SharePoint list pagination is the SPListItemCollectionPosition class.The constructor of SPListItemCollectionPosition takes one string parameter called PagingInfo. This PagingInfo parameter gets or sets the paging information used to fetch the next page of data or previous page of data.

Implementation:

I am going to show its implementation through asp.net(c#).

1.Changes required in ascx file:

In ascx file, take an asp repeater control to display the data in the page.

Example:

<table>
   <asp:Repeater ID=”Repeater1″ runat=”server”>
       <HeaderTemplate>
           <thead>
               <tr>                       
                   <th>Name</th>
                   <th>Email</th>
                   <th>City</th>
                   <th>Address</th>                
               </tr>
           </thead>
       </HeaderTemplate>
       <ItemTemplate>
           <tr role=”row” class=”odd”>                                                         
               <td><%# DataBinder.Eval(Container, “DataItem.Name”)%></td>
               <td><%# DataBinder.Eval(Container, “DataItem.Email”)%></td>
               <td><%# DataBinder.Eval(Container, “DataItem.City”)%></td>
               <td><%# DataBinder.Eval(Container, “DataItem.Address”)%></td>                
           </tr>
       </ItemTemplate>
       <AlternatingItemTemplate>
           <tr role=”row” class=”even”>
               <td><%# DataBinder.Eval(Container, “DataItem.Name”)%></td>
               <td><%# DataBinder.Eval(Container, “DataItem.Email”)%></td>
               <td><%# DataBinder.Eval(Container, “DataItem.City”)%></td>
               <td><%# DataBinder.Eval(Container, “DataItem.Address”)%></td>   

           </tr>
       </AlternatingItemTemplate>
   </asp:Repeater>
</table>

Also, take two link button controls on click of which next page information or previous page information will be fetched. One label control is taken to display the range of items displayed at a time.

Example:

<div>

    <asp:LinkButton ID=”LinkButtonPrevious” runat=”server” OnClick=”LinkButtonPrevious_Click”>Previous</asp:LinkButton>

       

    <asp:Label ID=”LabelPaging” runat=”server” Text=”Label”></asp:Label>

       

    <asp:LinkButton ID=”LinkButtonNext” runat=”server” OnClick=”LinkButtonNext_Click”>Next</asp:LinkButton>

</div>

 

  1. Changes in .cs file:

The  code to fetch the data from sharepoint list and applying pagination into it is shown as follows:

On pageload, we will take session variables and set them as shown.

       protected void Page_Load(object sender, EventArgs e)

       {

           if (!IsPostBack)

               {

                   int pageSize = 10;     

                   HttpContext.Current.Session.Clear();

                   HttpContext.Current.Session[“pageIndex”] = 1;

                   HttpContext.Current.Session[“position”] = null;

                   HttpContext.Current.Session[“nextPagingInfo”] = null;

                   HttpContext.Current.Session[“previousPagingInfo”] = null;

               }

               DisplayData();

        }

Here, we are first clearing the sessions. ‘pageSize’ session variable will be used for fetching the number of items at a time from the sharepoint list(here that is taken as 10). Pagination splits the whole data into number of sections(each containing the number of items as pageSize). ‘pageIndex’ =1 here means the 1st section. ‘nextPagingInfo’ will be used to store the next 10 set of items and ‘previousPagingInfo’ will be used to store the previous 10 set of items.

DisplayData() method will be used to fetch data from sharepoint list and bind to repeater.

 

 private void DisplayData()

       {

               Repeater1.DataSource=GetDataFromCandidateInformationList();

               Repeater1.DataBind();           

       }

//Method to fetch the data from sharepoint list

  public List<candidateInfoObj> GetDataFromCandidateInformationList(string Search)

       {

           List<candidateInfoObj> candidateInformationtList = new List<candidateInfoObj>();

           try

           {

               using (SPSite site = new SPSite(SPContext.Current.Site.Url))

               {

                   using (SPWeb web = site.OpenWeb())

                   {

                       SPList _canInfoList = web.Lists[“EmployeeInfo”];   //Sharepoint List name

                       SPQuery spqry = new SPQuery();   

                     

                       spqry.ListItemCollectionPosition = HttpContext.Current.Session[“position”] != null ? (SPListItemCollectionPosition)HttpContext.Current.Session[“position”] : null;                        

                       spqry.Query = “<Where><FieldRef Name = Status/><Value Type = ‘Text’>Active</Where>”;

                       spqry.RowLimit = Convert.ToUInt32(pageSize);

                       SPListItemCollection _candidateInfoItemsColl = canInfoList.GetItems(spqry);

 

                       foreach (SPListItem candidateInfoitem in _candidateInfoItemsColl)

                       {

                               candidateInfoObj Obj = new candidateInfoObj()

                               {

                                  Name = Convert.ToString(candidateInfoitem[“Name”]),

Email= Convert.ToString(candidateInfoitem[“Email”]),

City= Convert.ToString(candidateInfoitem[“City”]),

Address= Convert.ToString(candidateInfoitem[“Address”]),

                               };

                          }

                      

                       managePagerControl(_candidateInfoItemsColl);

                   }

               }

           }

           return candidateInformationtList;

       }

//Method to manage the next and previous paging info

public void managePagerControl(SPListItemCollection collListItem)
       {
           if (collListItem.ListItemCollectionPosition != null)
           {
               HttpContext.Current.Session[“nextPagingInfo”] = collListItem.ListItemCollectionPosition.PagingInfo;
           }
           else
           {
               HttpContext.Current.Session[“nextPagingInfo”] = null;
           }
           //The following code line shall add page information between the next and back buttons
           LabelPaging.Text = ((((int)HttpContext.Current.Session[“pageIndex”] – 1) * pageSize) + 1) + ” – ” + (((int)HttpContext.Current.Session[“pageIndex”] * pageSize) – (pageSize – collListItem.Count));            
           IEnumerator list = collListItem.GetEnumerator();
           list.MoveNext();
           SPListItem item = (SPListItem)list.Current;            
           HttpContext.Current.Session[“previousPagingInfo”] = “PagedPrev=TRUE&Paged=TRUE&p_ID=” + item.ID;  
           if ((int)HttpContext.Current.Session[“pageIndex”] <= 1)
           {
               LinkButtonPrevious.Enabled = false;
           }
           else
           {
               LinkButtonPrevious.Enabled = true;
           }
           if (HttpContext.Current.Session[“nextPagingInfo”] != null)
           {
               LinkButtonNext.Enabled = true;
           }
           else
           {
               LinkButtonNext.Enabled = false;
           }
       }

 

//Next and Previous buttons onclick events

 protected void LinkButtonNext_Click(object sender, EventArgs e)

       {

               HttpContext.Current.Session[“pageIndex”] = (int)HttpContext.Current.Session[“pageIndex”] + 1;

               if (HttpContext.Current.Session[“nextPagingInfo”] != null)

               {

                   HttpContext.Current.Session[“position”] = new SPListItemCollectionPosition((string)HttpContext.Current.Session[“nextPagingInfo”]);                  

               }

               else

               {

                   HttpContext.Current.Session[“position”] = null;

               }

               DisplayData();

           

       }

       protected void LinkButtonPrevious_Click(object sender, EventArgs e)

       {     

               HttpContext.Current.Session[“pageIndex”] =                        (int)HttpContext.Current.Session[“pageIndex”] – 1;

               HttpContext.Current.Session[“position”] = new SPListItemCollectionPosition((string)HttpContext.Current.Session[“previousPagingInfo”]);

               DisplayData();

       }

 

The output would be like this:

About The Author

Leave a Reply

*