11

We are trying to implement integration between a web application and SharePoint Online using Microsoft Graph rest API.

Specifically, we need to upload a file to a specific SharePoint site's document library (drive), different than current user default drive. We get the access token through Azure AD with access to all files.

We can upload files into any drive using /v1.0/me/drive/... but not when we use another drive.

For example:

var response = client.PutAsync(graphResourceUrl +
    "/beta/sharepoint/sites/" + siteCollSiteId +
    "/lists/" + listId +
    "/drive/root/children/" + fileName + ":/content",
    new ByteArrayContent(fileBytes)).Result;

var response = client.PutAsync(graphResourceUrl +
    "/beta/drives/" + "/" + listId +
    "/items/" + siteCollSiteId + "/" + fileName + ":/content",
    new ByteArrayContent(fileBytes)).Result;

var response = client.PutAsync(graphResourceUrl +
    "/beta/drives/" + listId + "/" + fileName + ":/content",
    new ByteArrayContent(fileBytes)).Result;

Both /v1.0 and /beta (in the case of SharePoint containing path) we are getting an error response of Not Implemented.

What could we be doing wrong? Is it not yet working with Microsoft Graph (other than /me)?

Marc LaFleur
  • 31,987
  • 4
  • 37
  • 63
danfer
  • 371
  • 1
  • 3
  • 10
  • Which auth scopes did you request? You need to have 'Sites.ReadWrite.All' to access document libraries outside of the user's default drive. – Ryan Gregg Jan 04 '17 at 02:28
  • Thanks @ryan-gregg, yes, we are using that permission for Graph at the Azure AD app, its shows up as "Read and write files in all site collections (preview)". Best regards, Danfer. – danfer Jan 06 '17 at 03:50

3 Answers3

18

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

Brian Smith
  • 1,467
  • 15
  • 31
  • How to upload any kind of file like png, jpeg, mp3 or mp4? – Neha Sharma Dec 27 '18 at 05:19
  • SharePoint / OneDrive doesn't block those file types. Normally, the File Upload control you are using will have the ability to block / only allow certain file types. – Brian Smith Dec 27 '18 at 20:24
  • Hi! I am able to upload a file. But that uploaded file won't open. I am sending content as binary. Tried with image, document. Any suggestion please. – Priyanka Arora Jun 05 '20 at 12:25
  • Here are a couple of repos that you can use as an example. https://github.com/smithbrianscott/microsoft-graph-files-app (Angular) https://github.com/smithbrianscott/microsoft-graph-files-web-forms (C#) – Brian Smith Jun 09 '20 at 03:23
1

Remove : from :/content Generally it's better for me to get driveId of the sp library first, then just work on the v1.0 endpoint with /v1.0/drive/{driveId}/

Bartosz J
  • 178
  • 4
  • Sorry but, when using suggestion of /v1.0/drive/{driveId}/ I'm getting 400 bad request response trying to upload file to a drive in a sharepoint site I have access to. Regards, Danfer. – danfer Jan 03 '17 at 15:20
  • 1
    @danfer Pay attention to details (driveS not drive) `final String endpoint = String.format("%s/drives/%s/items/%s/children/%s/content", Config.GRAPH_RESOURCE_URL, driveId, itemId, targetName);` – Bartosz J Jan 04 '17 at 14:10
  • 1
    you would use /v1.0/drives/{driveid} don't forget the 's' in drives – Brian Smith Oct 06 '17 at 21:22
0
curl -X PUT -H "Authorization: Bearer ${TOKEN}" -H "Content-Type: multipart/form-data" \
        -T "dump.sql" \
        https://graph.microsoft.com/v1.0/drives/${DRIVE_ID}/root:/Folder/dump.sql:/content
  • 1
    Although this code might answer the question, I recommend that you also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes. – Mark Rotteveel Aug 14 '23 at 11:12