Upload a File to SharePoint using Azure Graph API
In this post i am going to talk about Azure Graph API, specifically how to use graph api to upload file to SharePoint.
Microsoft’s documentation for upload functionality available over here is not very helpful. There are two important areas where clarity is lacking proper permissions on Graph API for Azure AD enterprise app and Upload endpoint documentation itself.
In this post we will using Postman to call graph api and we will cover two scenarios uploading file less than 4MB and greater than 4MB. We will be using client credentials flow, meaning we will be performing all these operations under application context instead of user context. Client credentials flow is generally used for daemon application/server to server communication.
First step is to create Azure AD enterprise Application. This is well documented and you can follow steps given here and here (follow steps given in section “Create new application secret”).
Now in Azure Portal navigate to Azure AD- App Registrations — Search for the App created in above step — API permissions. Ensure that following permission are granted and admin consent for the same is provided
Now follow steps given below to upload file
Generate Token
GET https://login.microsoftonline.com/{{tenant-id}}/oauth2/v2.0/tokenBody: x-www-form-urlencodedgrant_type:client_credentials
client_id:{{client-id}}
scope:https://graph.microsoft.com/.default
client_secret:{{client-secret}}#tenant-id: Azure AD directory ID or domain name#client-id: Application id of Azure AD enterprise app#client-secret: Application secret of Azure AD enterprise app
Get Drive ID/Document Library
Following request will return all the document libraries from the all the sites. From response get the drive id of the document library where file needs to be uploaded
GET https://graph.microsoft.com/v1.0/drivesHeader:“Authorization” : “Bearer <token obtained from above step>
Upload Document Smaller than 4MB
PUT https://graph.microsoft.com/v1.0/drives/{{drive-id}}/items/root:/{{file-name}}:/contentHeader:“Authorization” : “Bearer <token obtained from above step>Body: binary (select binary option in body in postman)Upload a file using select file optionfile-name: is file name along with extension example: test.txt
Upload Document Greater than 4MB
Uploading file greater than 4MB is two step process, first you have to create a session and then use upload url to upload file. If you are uploading file greater than 60MB then you will have to break the file down into smaller chunks each chunk should not exceed 60MB.
Create Session
POST: https://graph.microsoft.com/v1.0/drives/{{drive-id}}/items/root:/{{file-name}}:/createUploadSessionHeader:“Authorization” : “Bearer <token obtained from above step>This will return upload urlUpload DocumentPUT <Upload url received in above step>Header:“Authorization” : “Bearer <token obtained from above step>Body: binary (select binary option in body in postman)
Upload a file using select file option
I hope this helps