Search code examples
c#ajaxsharepointpaginationweb-parts

Unexpected results when using .net ajax & paging in sharepoint web part with c#


Alright so when the web part loads I pull from a list like so in the CreateChildControls() function:

String userGroup = GetProfileProperty(SPContext.Current.Web.CurrentUser.LoginName);//get user login's newsgroup // "All";

        if (!string.IsNullOrEmpty(userGroup) && !userGroup.Equals("All"))
        {
            qry.RowLimit = 5;
            camlquery = "<Where><Contains><FieldRef Name='Intended_x0020_Audience' /><Value Type='MultiChoice'>" + userGroup + "</Value></Contains></Where><OrderBy><FieldRef Name='Start_x0020_Date' Ascending='False' /></OrderBy>";
            qry.Query = camlquery;
            items = newsList.GetItems(qry);
            //Print items inside a ajax panel....


        }
        else
        {
            qry.RowLimit = 5;
            camlquery = "<OrderBy><FieldRef Name='Start_x0020_Date' Ascending='False' /></OrderBy>";
            qry.Query = camlquery;
            items = newsList.GetItems(qry);
            ////Print items inside a ajax panel....
        }

I do not use paging yet, but I have stored the query list in a var items. When the user clicks a button the update panel clears out the current controls and does another query to get the new items, but this time I use paging.(see below)

        int lastID = items[items.Count - 1].ID;//last news item id of current page

        //Get Web Context
        SPWeb web = SPContext.Current.Web;

        //Clear old panel
        UpdatePanel up = (UpdatePanel)FindControl("Panel");
        up.ContentTemplateContainer.Controls.Clear();//clear container of old news items

        SPQuery qry = new SPQuery();//get query

        qry.RowLimit = 5;
        string camlquery = "<OrderBy><FieldRef Name='Start_x0020_Date' Ascending='False' /></OrderBy>";
        qry.Query = camlquery;
        SPListItemCollectionPosition position = new SPListItemCollectionPosition("Paged=TRUE&p_ID=" + lastID.ToString());
        qry.ListItemCollectionPosition = position;
        items = web.Lists["News Wire"].GetItems(qry);// newsList.GetItems(qry);

        //print items inside ajax panel....

It does change the page, but it returns more items than necessary. I'll get five when the webpart first loads, but when I click the button that triggers the event to get more list records, I get the first item of the next page and also the first page's first 3 items. Note: My list only has six items in it. I limited my RowLimit to 5, but when I load the second page I get 4 items and 3 from the first page.


Solution

  • If you're paging forwards, you can pass the ListItemCollectionPosition from your first query's results (SPListItemCollection) to the next query - you don't have to mess about constructing it by hand.

    You'll need to store it somewhere after you get the first set of results - I've used ViewState before, you could use a hidden control in the update panel as well.

    When you're paging backwards, you do need to create the string as you are doing. You also need to include a key/value to indicate that you're going backwards, and you need to include an indication of your sort. (You might be able to get this from the auto-generated forward-paging value.) (I think missing the sort is why your forwards paging isn't working, too.)

    This is the resource I used when I was trying to get this working myself, although it's a little hazy now.