MuleSoft SharePoint API integration
- April 22, 2020
SharePoint helps organizations share and manage content, knowledge and applications to empower teamwork, quickly find information and seamlessly collaborate across the organization.
MuleSoft SharePoint API allows developers to access, add and modify from anywhere at any time.
This article explains how to get started with SharePoint API to upload files, update metadata and download files.
We know that when accessing SharePoint content we need the appropriate credentials. Below are the ways to obtain them:
- Basic Username and Password
- Oauth JWT
- Oauth SAML
- Oauth Username and Password
- Oauth 2.0
I am going to connect to SharePoint through Oauth 2.0.
To get a token from Sharepoint Oauth server, we need to do App registration in Microsoft Azure Oauth Provider.
SharePoint App registration
To register an app in SharePoint navigate to the “New App Registration” page. The URL of that page will be similar to:
- Fill the details based on the screenshot below Screenshot – 1.
- Copy the generated client id and client secret.
- App successfully registered. We need to provide the permission so that it can access the data.
- Go to the App Id and click on lookup Screenshot – 2.
- In “Permission Request XML” paste the following content it has FULL control access Screenshot – 2, create and trust it.
Permission request XML
<AppPermissionRequests AllowAppOnlyPolicy="true">
<AppPermissionRequest Scope="http://sharepoint/content/sitecollection/web" Right="FullControl"/>
</AppPermissionRequests>
Screenshot – 1
Screenshot – 2
Retrieve token
Now it’s time to generate a token using MuleSoft HTTP Requester from MS Azure Oauth Provider.
Please refer to the below configuration for the information, which will be required to generate the token.
URL | https://accounts.accesscontrol.windows.net/<TenantID>/tokens/OAuth/2 |
Content Type | application/x-www-form-urlencoded |
grant_type | client_credentials |
resource | resource/SiteDomain@TenantID |
client_id | ClientID@TenantID |
client_secret | ClientSecret |
HTTP Request Configuration
HTTP Request Body
HTTP Requester Output
{
"token_type": "Bearer",
"expires_in": "86399",
"not_before": "1587197160",
"expires_on": "1587283860",
"resource": "00000003-0000-0ff1-ce00-000000000000/******.sharepoint.com@<TenantID>",
"access_token": "Token"
}
This Token we will use for a SharePoint API, which will help us integrate with SharePoint Data.
File Upload Operation
Notes
- payload.Name – File name which will be uploading
- Use ListItemAllFields as expand because it gives us the file uploaded id, which is the unique identifier to refer file for subsequent operation.
Base Path | ******.sharepoint.com |
Port | 443 |
Path | #[“/_api/Web/GetFolderByServerRelativeUrl(‘/sites/Docs/Contracts’)/Files/add(url='”++ payload.Name ++”‘,overwrite=true)?\$expand=ListItemAllFields”] |
Headers | Authorization: Bearer Token, Accept: “application/json” |
BodyMethod | File Content as BinaryPUT |
HTTP Requester configuration to upload file
Id is retrieved from Response payload, which will be used for updating metadata.
File Update MetaData
I’m going to update file metadata as “SF Reference number” using SharePoint API.
Refer screenshot SF Reference number
Notes – vars.vId we got from file upload api using expand = ListItemAllFields
Base Path | ******.sharepoint.com |
Port | 443 |
Method | POST |
Path | #[“/_api/lists/getbytitle(‘Contracts’)/items(” ++ vars.vId ++ “)”] |
Body | Please see below as I have write DataWeave code |
Headers | |
“Authorization” : Bearer Token | |
“Accept” : “application/json;odata=verbose” | |
“Content-Type” : “application/json;odata=verbose” | |
“X-HTTP-Method” : “MERGE” | |
“If-Match” : “*” |
HTTP Request Body
%dw 2.0
output application/json encoding = "iso-8859-1"
---
{
"__metadata": {
"type": "SP.Data.ContractsItem"
},
"SF_x0020_Reference_x0020_Number": vars.accountId
}
HTTP Requester configuration Update Meta Data
That’s exactly how we update the file metadata.
— By Shekh Muenuddeen