SharePoint REST JSON results differ with $top

When you have been working with the SharePoint 2010 REST API, you know it’s extremely easy to get the results you want, using jQuery and calling the URL with REST syntax from JavaScript.

Because JSON data is so much easier to use and parse than XML, I almost always use the $.getJSON functionality to get the data I want.
If you use FireFox and FireBug (of course you do!), you probably have taken a look at the data underneath. Something like this (click to enlarge):

As you can see, the REST URL is something like this:

?$expand=Country&$orderby=StartTime asc

The resulting JSON data is usually processed in a loop:

$.each(data.d.results, function(i,result) { do something... }

You just iterate through ‘data.d.results’. Easy.

This worked perfectly until last week. The customer asked me to limit the results to the first 10 items. So I added the querystring parameter $top, like this:

?$expand=Country&$orderby=StartTime asc&$top=10

That should do the trick. But it didn’t… I got a JavaScript error.
Looking at the data in FireBug, I discovered that the resulting data in JSON changes when you add the querystring parameter $top.

To my surprise, the ‘results’ part from the expected ‘data.d.results’ was gone!

So now, the resulting JSON data must be processed in a loop:

$.each(data.d, function(i,result) { do something... }

Nothing scary, but obviously a bug in the SharePoint 2010 REST API.

One thought on “SharePoint REST JSON results differ with $top

  1. Christian_M

    Thank you so much for this post! I thought I was going crazy: nothing was displayed on the page as soon as the $top parameters was added. Thanks for the workaround.


Leave a Reply

Your email address will not be published. Required fields are marked *