Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

Let's assume I have list with 100 items. Is it possible to write CAML query to get 5 items, but skip 10 items from the beggining? Something like LIMIT and OFFSET in SQL?

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

Unfortunately, SharePoint paging works a bit differently than LIMIT and OFFSET in SQL. To get paginated items you have to provide ID of the first item for the page you want and then set RowLimit, and if you have sorting defined for the list, you also have to provide sorting columns names and values of those columns for the first item on your page.

You can see how it works if you go to any list which has enough items and try to go through pages and see how the query string of the page changes. Then delete some items and try again. Then change order of items and try again...

All those params you will have to pass into ListItemCollectionPosition property of SPQuery object.

More about paging in SharePoint:

http://www.directsharepoint.com/2011/03/step-by-step-guide-to-implement-paging.html

In your particular case, you can simplify things a bit, if you don't have filtering and sorting:

  1. Select first 11 IDs from the list
  2. Use 11th ID to create the correct ListItemCollectionPosition for your SPQuery
  3. Set RowLimit of the SPQuery object to 5

To quickly select top 11 IDs, use this query:

var query = new SPQuery()
{
    ViewFields = "ID",
    ViewFieldsOnly = true,
    IncludePermissions = false,
    RowLimit = 11
};
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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