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:
- Microsoft 365 Developer account
- Anypoint Studio 7.X (X>=6)
- Anypoint Platform account
Follow the steps below to upload a file to SharePoint
1. Open Microsoft 365 Developer Azure Active Directory admin center.

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.

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

4. Navigate to API permissions, and assign the below permission to the app.
Microsoft Graph → User.ReadWrite.All , Sites.FullControl.All

5. Download the OAuth module from Exchange.



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



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.


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