Reputation: 593
Forgive me, I'm very new to using REST.
Currently I'm using SP2013 Odata (_api/web/lists/getbytitle('<list_name>')/items?)
to get the contents of a list. The list has 199 items in it so I need to call it twice and each time ask for a different set of items. I figured I could do this by calling:
_api/web/lists/getbytitle('<list_name>')/items?$skip=100&$top=100
each time changing however many I need to skip. The problem is this only ever returns the first 100 items. Is there something I'm doing wrong or is $skip
broken in the OData service?
Is there a better way to iterate through REST calls, assuming this way doesn't work or isn't practical?
I'm using the JSon protocol with the Accept
Header equaling application/json;odata=verbose
I suppose the $top=100
isn't really necessary
Edit: I've looked it up more and, I'm not entirely sure of the terms here, but using $skip
works fine if you're using the method introduced with SharePoint 2010, i.e., _vti_bin/ListData.svc/<list_name>?$skip=100
Actually, funny enough, the old way doesn't set a 100 item limit on returns. So skip isn't even necessary. But, if you'd like to only return a certain segment of data, you'd have to do something like:
_vti_bin/ListData.svc/<list_name>?$skip=x&$top=(x+y)
where each time through the loop you would have something like x+=y
You can either use the old method which I described above, or check out my answer below for an explanation of how to do this using SP2013 OData
Upvotes: 13
Views: 11245
Reputation: 41
Don't forget that in order to use __next you need to have a
$skiptoken=Paged=TRUE
in the url as well.
Upvotes: 2
Reputation: 593
Alright, I've figured it out. $skip
isn't a command which is meant to be used at the items?
level. It works only at the lists?
level. But, there's a way to do this, actually much easier than what I wanted to do.
If you just want all the data
In the returned data, assuming the list you are calling holds more than 100 items, there will be a __next
at d/__next
(assuming you are using json). This __next
(it is a double underscorce, keep that in mind. I had a few problems at first because I was trying to get d/_next
which never returned anything) is the right URL to get the next set of items. __next
will only ever be a value if there is another set of items available to get.
I ended up creating a RequestURL
variable which was initially set to to original request, but was changed to d/__next
at the end of the loop. Then the loop went and checked if the RequestURL
was not empty before going inside the loop.
Forgive my lack of code, I'm using SharePoint Designer 2013 to make this, and the syntax isn't horribly descriptive.
If you'd only like a small set of data
There's probably a few situations where you would only want x
amount of rows from your list each time you go through the loop and that's real easy to do as well.
if you just add a $top=x
parameter to your request, the __next
URL that comes back with the response will give you the next x
rows from your list. Eventually when there are no rows left to return __next
won't be returned with the response.
Upvotes: 13