2
votes

I'm attempting to modify the node.js Quick Start example to produce a list of the user's excel documents, but I'm running into issues.

This query works (returns a non-empty result) in the Graph Explorer:

https://graph.microsoft.com/v1.0/me/drive/root/search(q='.xlsx')?select=name,id,webUrl

I have tried a few ways to reproduce it in graphHelpers.js. This query returns an empty result:

function getExcelDocs(accessToken, callback) {
  // Get list of excel docs in the user's onedrive
  request
   .get("https://graph.microsoft.com/v1.0/me/drive/root/search(q='.xlsx')?select=name,id,webUrl")
   .set('Authorization', 'Bearer ' + accessToken)
   .end((err, res) => {
     // Returns 200 OK and the search result in the body. 
     callback(err, res);
   });
}

If I substitute the search URL with the drive children query, I get back a non-empty set of documents.

.get('https://graph.microsoft.com/v1.0/me/drive/root/children')

Is there a problem with how I'm formatting the query url?

1
Have you tried encoding the URL (i.e. encodeURIComponent() )?Marc LaFleur
What part of the URL would you suggest encoding? Clearly the get request will fail if I encode the colon and slashes. I will try encoding the brackets and single-quotes.Robert Sim
Unfortunately this returned a bad request result.Robert Sim
I'm not familure with this graphHelpers.js library but it sounds like it may be unable to handle the search query correctly. Do you have a link to the graphHelpers.js project? Its hard to tell without looking at the code itself.Marc LaFleur

1 Answers

0
votes

The parens in the URI are causing the problem here. The content within those parens represents the body of the request. The underlying library that is processing this request (SuperAgent) doesn't seem to like them going directly into the URI.

I believe you can fix this by providing the URL components separately:

request
  .get("https://graph.microsoft.com/v1.0/me/drive/root/search", "q='.xlsx'")
  .query({ select: 'name,id,webUrl'})
  .set('Authorization', 'Bearer ' + accessToken)
  .end((err, res) => {
      // Returns 200 OK and the search result in the body. 
      callback(err, res);
  });

Alternatively, this might also work:

request
  .get("https://graph.microsoft.com/v1.0/me/drive/root/search")
  .query({ select: 'name,id,webUrl'})
  .send("q='.xlsx'")
  .set('Authorization', 'Bearer ' + accessToken)
  .end((err, res) => {
      // Returns 200 OK and the search result in the body. 
      callback(err, res);
  });