Upload file to SharePoint drive using Microsoft Graph

In order to get all the files of a drive using v1.0, you would first need to get an access token, then get the ‘drive-id’ and use the following URL (note: it is ‘drives’ not ‘drive’):

https://graph.microsoft.com/v1.0/drives/{drive-id}/root/children

To get the drive id, make the following GET request using postman, this will list all the drives on the site, and you will be able to get the ID of that drive:

https://graph.microsoft.com/v1.0/sites/{tenant}.sharepoint.com:{path-to-site(ie: /sites/HR)}:/drives

To answer your question regarding the uploading of files, you will make a PUT request to the following URL:

https://graph.microsoft.com/v1.0/drives/{drive-id}/root:/{folder-name}/{file-name.txt}:/content

You will need to set two required headers:

  • Authorization
  • Content-Type

Next, you will pass the binary stream of the file into the body of the request.

Other helpful items

Get all files inside of a folder:

https://graph.microsoft.com/v1.0/drives/{drive-id}/root:/{folder-name}:/children

Get content of users OneDrive:

https://graph.microsoft.com/v1.0/me/drive/root/children

REFERENCE:
https://developer.microsoft.com/en-us/graph/docs/api-reference/v1.0/api/driveitem_put_content#example-upload-a-new-file

Leave a Comment