Using Office 365 Graph API will allow you with few adaptations to download a document by using your favorite development language.

Your best friend Graph explorer

This tool very intuitive and easy to use is made to help you: https://developer.microsoft.com/en-us/graph/graph-explorer

graph explorer.png
On the left side simply log with your Office 365 account to be ready to use the tool, don’t forget to add SharePoint sample to quick start your development.

Get SharePoint site Id

First thing first you have to get the SharePoint site Id where your file is located, from this url:
https://graph.microsoft.com/v1.0/sites/{host-name}:/{server-relative-path}
Replace:

  • {host-name} with your SharePoint root site url in my case by “masaogroup.sharepoint.com”
  • {server-relative-path} with sites collection root site if you are not using your SharePoint root tenant site.

After clicking on “Run query” if you don’t make any mistake your will see your site informations:

site id.png

So my site collection id is “masaogroup.sharepoint.com,b10d0761-01e2-47d0-b604-223322725e41,52596943-0f73-4f73-9695-2ac1961f1dfa

We will now use this id for the next actions, my based url will be:
https://graph.microsoft.com/v1.0/sites/masaogroup.sharepoint.com,b10d0761-01e2-47d0-b604-223322725e41,52596943-0f73-4f73-9695-2ac1961f1dfa

Enumerate “Drives”

For a SharePoint site a Drive is a document library by using this url:
https://graph.microsoft.com/v1.0/sites/{site-id}/drives

You will get a list of Drives with the following fields:

  • createdDateTime
  • description
  • id
  • lastModifiedDateTime
  • name
  • webUrl
  • driveType
  • createdBy
    • user
      • displayName
  • lastModifiedBy
    • user
      • displayName
  • owner
    • user
      • email
      • id
      • displayName
  • quota

drive id

We will store the “id” from the drive in order to get specific document by using id or document path from document library root url.

Get file from drive

For drive url we will now used the following structure: https://graph.microsoft.com/v1.0/sites/{site-id}/drives/{drive-id}

In order to get detail from a specific document based on path use the following structure:

https://graph.microsoft.com/v1.0/sites/{site-id}/drives/{drive-id}/root:/{item-path}

To get my file on my document library root:
document.png

I will use this url:
https://graph.microsoft.com/v1.0/sites/{site-id}/drives/{drive-id}/root:/document.docx

From the response:
document url

use the field “@microsoft.graph.downloadUrl” in order to download the file, this link will allow you to get the file without any authentification token.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s