8

I am trying to implement client side paging using Microsoft OneDrive API/SDK. For that I need the total count of items as response from the API, and based on the skip and maximum limit value passed to the API, the response should be fetched.

In the List Items link, it is mentioned that we can achieve this using the query strings provided here. Based on this assumption, I am building the URL for API call as below:

string.Format("https://graph.microsoft.com/v1.0/me/drive/root/children?$skip={0}&$top={1}&$count=true",topValue*page, topValue)

Everything seems to be fine as per the info in the above mentioned URL, but I am getting "Bad Request" from the server with error message as shown below:

{
  "error": {
    "code": "",
    "message": "The query specified in the URI is not valid. Query option 'Skip' is not allowed. To allow it, set the 'AllowedQueryOptions' property on EnableQueryAttribute or QueryValidationSettings.",
    "innerError": {
      "request-id": "384693d7-65bd-4dc6-8d60-afde68e01555",
      "date": "2017-04-25T10:28:15"
    }
  }
}


{
  "error": {
    "code": "",
    "message": "The query specified in the URI is not valid. Query option 'Count' is not allowed. To allow it, set the 'AllowedQueryOptions' property on EnableQueryAttribute or QueryValidationSettings.",
    "innerError": {
      "request-id": "2188a06f-10cf-402c-9c49-bd296b9db614",
      "date": "2017-04-25T10:29:05"
    }
  }
}

Can this be achieved using REST APIs or Microsoft Graph SDK?

PS: I saw the concept of skipToken but that won't fit into our requirements as it does not return the total count and only incremental navigation is supported.

Saket Kumar
  • 4,363
  • 4
  • 32
  • 55
  • 3 asks: 1. show us the code where you set AllowedQueryOptions, 2. how large can `topValue*page` become? Bigger than int? 3. have you tried putting `skip` after `format` – Reuben Tanner May 09 '17 at 14:53
  • 1.) AllowedQueryOptions is available while working with OData in Web API. It can't be used to make simple RESTful calls, if it can be please show me the way. 2.) The maximum value of topValue*page will depend on the number of files and folders OneDrive allows. If OneDrive allows users to have files and folders greater than integer, then yes, it's value can be greater than int. 3.) I am not using $format param in my query, I don't think OneDrive API even has this parameter. – Saket Kumar May 10 '17 at 06:56
  • sorry, I meant `$top` not `$format`...also, why the -1 for attempting to help? – Reuben Tanner May 10 '17 at 14:57
  • I have tried changing the $top positions, it still didn't work. My bad for -1, reverted it. Really appreciate your efforts to help. Thanks. – Saket Kumar May 11 '17 at 05:52
  • No problem. Thanks for reverting the -1. I just pinged the OneDrive engineer that I quoted in the answer...it looks like he has replied. – Reuben Tanner May 11 '17 at 15:17
  • Thanks for your help. – Saket Kumar May 12 '17 at 07:16

1 Answers1

1

It appears that a OneDrive engineer has already answered this question here:

OneDrive's paging model is a little different to skip+take. Essentially you'll make a query like:

GET https://graph.microsoft.com/v1.0/me/drive/root/children?$top=5

and in the response you should see the usual array of values, along with a property called @odata.nextLink. You'll want to take that URL use it request the next page:

"@odata.nextLink": "https://graph.microsoft.com/v1.0/me/drive/root/children?$skiptoken=ASDGASGSD"

GET https://graph.microsoft.com/v1.0/me/drive/root/children?$skiptoken=ASDGASGSD

You keep doing this until you don't get an @odata.nextLink returned.

Community
  • 1
  • 1
Reuben Tanner
  • 5,229
  • 3
  • 31
  • 46
  • As far as I am aware, AllowedQueryOptions is the property of OData in ASP.Net Web API. I am not using System.Web.Http.OData.Query to call the OneDrive endpoints. I am making simple RESTful call to OneDrive endpoints as described in URL: https://dev.onedrive.com/items/list.htm. While making a call to above endpoint using simple HttpClient, or other third party libraries, I don't think AllowedQueryOptions is available. – Saket Kumar May 10 '17 at 06:45
  • Morover, I also verified in MSDN https://msdn.microsoft.com/en-us/library/system.web.http.odata.query.odatavalidationsettings.allowedqueryoptions(v=vs.118).aspx. The default is all query options, including $filter, $skip, $top, $orderby, $expand, $select, $inlineCount, $format and $skiptoken. – Saket Kumar May 10 '17 at 06:48
  • The MSDN docs you showed state that the default is all query options _when using their client_. You may not be benefiting from that default by making a raw GET. – Reuben Tanner May 10 '17 at 14:58
  • 1
    I am aware of this incremental approach. But let's say, I have 2000 files and folders in the root, and I need to get the last 100 set. For this, I will need to make 20 calls to receive the results. Seems like a lot of load on the server. – Saket Kumar May 11 '17 at 05:55
  • 2
    Unfortunately, paging using custom ranges isn't supported to ensure we have efficiency at scale. I'd suggest creating (or voting) on idea at our [user voice page](https://onedrive.uservoice.com/forums/262982-onedrive/category/89523-developer). Theoretically you could get the last 100, but specifying a descending `orderby` with a `top=100`, but that obviously only works for that specific example :). – Brad May 11 '17 at 14:57