Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upload file to SharePoint drive using Microsoft Graph

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)?

like image 487
danfer Avatar asked Dec 22 '16 14:12

danfer


People also ask

How do I upload a SharePoint file to graph API?

According to my research and testing, you can use the following Graph API to upload files to SharePoint drive: PUT /sites/{site-id}/drive/items/{parent-id}:/{filename}:/content.

Can you upload files to SharePoint?

In a Microsoft SharePoint document library, create a new file, upload your own, and then share it with others.

What can you use Microsoft Graph for?

Microsoft Graph is a RESTful web API that enables you to access Microsoft Cloud service resources. After you register your app and get authentication tokens for a user or service, you can make requests to the Microsoft Graph API.


2 Answers

In order to get all the files of a drive using v1.0, you would first need to get an access token (which I see you are already passed that), then get the 'drive-id' and use the following URL(note: its not 'drive' it is 'drives'):

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

To get the drive id, I made 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 Uploading 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

like image 167
Brian Smith Avatar answered Oct 07 '22 03:10

Brian Smith


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}/

like image 22
Bartosz J Avatar answered Oct 07 '22 03:10

Bartosz J