Download a file from SharePoint with the API

Reading time: 5 minutes

Many organizations are turning to SharePoint to store their files and share documents. Allowing your systems to access these files might seem like a challenge at first, but with the help of the Microsoft Graph API, you can quickly download the file to start processing on your back-end.  

Download the example solution to get started. Use the created functions in your solution to simplify Microsoft Graph integration.

The Process 

To download a file from a SharePoint location with Graph API we need to do a few things: 

  • Authenticate (Using OAuth2) to get the access token 
  • Get the Site ID to download from 
  • Get the Drive ID to download from  
  • Download the file via a REST API  
  • Write the received data into a binary file (this will be our “download”) 

There can be variations on the above workflow depending on what you need to do. This workflow assumes you know the site name and the file name to download. We are also assuming that the SharePoint setup is straight forward. If your SharePoint setup is a bit more complex, you may need to make amendments to retrieve subsites, and specific drives and to list available files.  

Getting Started  

We have built an example solution that illustrates how to interact with the Microsoft Graph API. You will need to provide your Tenant ID, Client ID and Client Secret. Ensure that the correct permissions are applied. You can see what permissions are required on the Microsoft Documentation. 

Once you have access sorted, it becomes a simple matter calling the correct endpoints. To download the file you need to retrieve the files download URL, retrieve the file and then write it to a folder by using the Linx binary file writer. 

Authentication 

Microsoft Graph uses OAuth2 as one of its authentication options, we decided to go with this option in our sample. You will need to provide the following details: 

  • Tenant ID 
  • Client ID 
  • Client Secret 
  • Scope: Depending on what service you call the scope will differ, for working with calendars, you will need to use https://graph.microsoft.com/.default 

If you are using the sample, it’s a simple matter of passing in those parameters into the GetToken function, the function will call the endpoint and return an Access Token for you to use in all other API calls. 

Get the file location details 

To download the file, its download URL must be retrieved. This is done by calling the Items endpoint. To accomplish this, the process will need a few things, mainly the DriveID. To do this, we built the Linx Sample solution that contains functions to: 

Retrieve Site details by passing in a site name. An assumption was made that the site name is known and can be passed in, this calls the Sites endpoint and will retrieve important information, mainly the Site ID. This step is redundant if you already know the Drive ID of the drive where the file is located.  

Get a file from the SharePoint API

Now use the Site ID to retrieve the Drives details. By using the Site ID, you can retrieve a list of all sites on that drive. This will also provide the Drive ID.  

After retrieving the Drive ID you can retrieve File details. The solution was built on the assumption that the file name is known. It contains a function with the name DownloadItemFromDrive, where the drive ID and File name are passed in (alongside save location where the file should be downloaded). The Items endpoint can return file details, including the file download URL.  

Download the file 

Any file can be downloaded by using a REST call, this means that if you have the download URL you can use the CallRESTEndpoint component in Linx to retrieve the file in a binary format. By setting the output type to List<Byte> and then writing that response to a file by using the BinaryFileWrite component, you can effectively download a file. Use the file name from the file details retrieved above to save the file.  

Download a file from a URI with REST

Sign up to our Newsletter