Upload File To SharePoint Using SharePoint API With OAuth

Author: Saddam Shaikh

In this blog, we will learn how to upload a file to SharePoint using SharePoint API with OAuth.

What is SharePoint?

SharePoint is a collaboration and document management platform that enables organizations to store and share documents or content across teams. SharePoint can be hosted online (Microsoft-managed) or on-prem (Customer-managed).

Prerequisites:
Follow the steps below to upload a file to SharePoint

1. Open Microsoft 365 Developer Azure Active Directory admin center.

Microsoft 365 Developer Azure AD config

2. Open App registrations to register a new app to get client_id and client_secret. Note down the client_id, client_secret, and tenant id.

Azure app config overview

3. In the Authentication section, add a callback URL on which auth code will be returned when performing the OAuth dance.

Azure app callback url config

4. Navigate to API permissions, and assign the below permission to the app.

Microsoft Graph → User.ReadWrite.All , Sites.FullControl.All

Azure app API permissions config

5. Download the OAuth module from Exchange.

Configure HTTP request to upload a file to SharePoint.
Configure HTTP request to upload a file to SharePoint.
HTTP Request OAuth Configuration for Authorization code grant type

6. Create a test Mule flow that reads a file from the local system and uploads it to the SharePoint Library.

Test Mule Flow
Transform Message — Convert File Content to Byte Array Config
HTTP Request General Config

7. Deploy the application and perform the OAuth dance by opening the Local authorization URL in the browser. This will generate the access token that would be used in subsequent API calls. This step will require you to sign in to your Microsoft account to grant permission.

8. Test the flow using Postman. We should be successfully able to upload a file to the SharePoint Documents library.

Postman Snapshot
SharePoint Document Snapshot
Limitations:

We can not upload file size greater than 250 MB using SharePoint API.

We use cookies on this site to enhance your user experience. For a complete overview of how we use cookies, please see our privacy policy.