Home > front end >  Onedrive list files in folder javascript
Onedrive list files in folder javascript

Time:10-27

I am trying to list all files in a folder on my onedrive using ajax. Is there universal url for this or do I need to go through their picker? Like google drive url for example:

var url = "https://www.googleapis.com/drive/v3/files?q='"   FOLDER_ID   "' in parents&pageSize=1000&key="   API_KEY

$.ajax({
        url: url,
        dataType: "jsonp"
    }).done(function(media) {

  
    }).fail(function(jqXHR, textStatus, errorThrown) {
    
    }); 

CodePudding user response:

I believe your goal is as follows.

  • You want to retrieve the file list in the specific folder of OneDrive using ajax.

In this case, how about the following modified script?

Modified script:

In this modified script, the file list is retrieved from the specific folder using Graph API. In this case, the access token is required to be used. When the access token is not used, an error like InvalidAuthenticationToken occurs even when the folder is publicly shared. Please be careful about this.

const folderName = "### folder name ###";
const driveId = "### your drive ID ###";
const url = `https://graph.microsoft.com/v1.0/drives/${driveId}/root:/${folderName}:/children`;
const accessToken = "### your access token ###";
$.ajax({
  url: url,
  headers: {"Authorization": "Bearer "   accessToken}
}).done(function(media) {
  console.log(media)
}).fail(function(jqXHR, textStatus, errorThrown) {
});
  • Also, you can retrieve the file list from the other endpoint as follows. In this case, drive id is not used.

      const folderName = "### folder name ###";
      const url = `https://graph.microsoft.com/v1.0/drive/items/root:/${folderName}?expand=children`;
    

References:

  • Related