Microsoft Graph: Download file via Rest API

Viewed 1820

I would like to download the contents of a sharepoint document via microsofts Graph API.

When I show the document's details on the sharepoint website, I see the following path in the document's details: My Company Team Site > Documents > My Application > file.xlsx

Approach 1:

Using the graph explorer, I can execute the following query: https://graph.microsoft.com/v1.0/sites/root/drives. It returns a single object called "Documents" with this WebURL: https://myCompany.sharepoint.com/Shared%20Documents

However, I don't know how I proceed from there and get to the folder's sub-elements, all my attempts caused HTTP errors.

Approach 2:

Alternatively, I also tried to "get a sharepoint site based on it's relative path": https://graph.microsoft.com/v1.0/sites/myCompany.sharepoint.com:/

This request also yields one result with the display name "My Company Team Site" and an ID looking like this: myCompany.sharepoint.com,45aaf871-b578-0000-...., but I'm not sure how to proceed from there and how the remaining URL should look like.

Question:

What's the correct approach? How can I iterate the "files within a folder" in sharepoint and how do I get the siteID/itemID of the documents/folders I'm looking for?

2 Answers

You are on the right track, lets try to connect the dots together.

It is assumed file named Guide.docx is located under Archive folder in library

Approach 1

However, I don't know how I proceed from there and get to the folder's sub-elements, all my attempts caused HTTP errors.

For this approach the following request demonstrates how to download a file:

GET https://graph.microsoft.com/v1.0/sites/root/drive/root:/archive/guide.docx:/content

where :/archive/guide.docx:/ is path-based addressing syntax used to target a file inside a folder

Note: there is no need to explicitly specify path to library (Shared Documents) since it is a default drive

Approach 2

This request also yields one result with the display name "My Company Team Site" and an ID looking like this: myCompany.sharepoint.com,45aaf871-b578-0000-...., but I'm not sure how to proceed from there and how the remaining URL should look like.

Since siteId is determined (myCompany.sharepoint.com,45aaf871-b578-0000..) the following request demonstrates how to address a file by site id and file path:

GET https://graph.microsoft.com/v1.0/sites{site-id}/drive/root:/archive/guide.docx:/content

References

Addressing resources in a drive on OneDrive

Get site id:

https://graph.microsoft.com/v1.0/sites/yourtenant.sharepoint.com:/sites/lee

Get the files(item id) in your folder. (folder under my test library->MyDOc/Folder)

https://graph.microsoft.com/v1.0/sites/siteid/drives/b!kan3K2m2N0Ww9Vn31kUuSLJVeywG43tAsaIcpv7Mme1FME9qbHnDRJrbrhnPTRCs/root:/folder:/children
Related