Here is the code Im using to retrieve the first 4 most upcoming announcements from the OOTB announcements list in sharepoint:

How do I modify this to retrieve the next 4 (5,6,7 and 8) most upcoming events preserving the filters and order bys in my CAML??

Is there a way for me to use an Index variable and iterate?

Or should I go with LINQ's skip and take features?? (I'd like to know how to do it using the CAML query though)

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

                    SPQuery spQuery = new SPQuery();

//The following CAML query filters to show the 4 most upcoming   announcements.                         
 spQuery.Query = " <Where><And><IsNotNull><FieldRef   Name='Title' /></IsNotNull><Or><IsNull><FieldRef Name='Expires' /></IsNull><Geq><FieldRef   Name='Expires' /><Value   Type='Date'><Today /></Value></Geq></Or></And></Where><OrderBy><FieldRef Name='Created'   Ascending='True' /></OrderBy>";

                    spQuery.RowLimit = 4;
                    SPListItemCollection oListCollection = list.GetItems(spQuery);

                    foreach (SPListItem item in oSPListItemCollection)
                        // Check if item exists
                        if (item != null)
                            Label1.Text = item["Title"].ToString();
  • you can get a list item by its index number, why dont use that Jul 13, 2012 at 19:38

2 Answers 2


SPQuery.ListItemCollectionPosition is used for paging in CAML queries, but you can only use saved values of it to go to the page after the query that returned the ListItemCollectionPosition

  • 9
    Isn't that just disappointing? Requiring the last item ID to show just sounds like a total retard way to do paging! Especially when there are deleted items. Why? Why??? It's like the APIs have been designed specifically to be an annoyance. How hard is it for the SharePoint team to provide a proper paging API? I want to know WHY they made this choice. Maybe to bug developers? Thanks for the answer anyways.
    – Lzh
    Jun 29, 2014 at 8:34

I'd say LINQ to SharePoint is the way to go, but you can also try this:

var query = new SPQuery();
query.Query = "<Where><Eq><FieldRef Name='field' /><Value Type='Text'>value</Value></Eq>  </Where><RowLimit>10</RowLimit><OrderBy><FieldRef Name='Title' Ascending='true' /></OrderBy>";
var position = new SPListItemCollectionPosition("Paged=TRUE&p_ID=1");//or whatever page
query.ListItemCollectionPosition = position;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.