Upload a file to SharePoint using Mendix

Heena S
Mendix Community
Published in
7 min readFeb 17, 2022

--

In business today, organizations need to collect and store their customer's information securely, in order to operate effectively. Most will consider storing their data or documents in the cloud safe and secure, all in one place, which can be shared with colleagues and partners. Let’s take a look at how to do this with Sharepoint and Mendix.

SharePoint is a cloud-based service, offering a secure location to store, organize, access, and share documents from any device.

To start, we need to know how to access SharePoint

Let’s begin with the basic setup

SharePoint is a product from the Microsoft office suite. To access SharePoint we need to create the Microsoft 365 developer account and join the Microsoft 365 developer program! and get a domain mail from the profile.

Before starting with integration steps, we will have a short introduction to Azure Active Directory.

Azure AD is a cloud-based identity and access management service, with a single set of login credentials a person can sign up to multiple services and access anywhere over the cloud.

Create an app in Azure Active Directory to start with the integration process.

Follow the steps to achieve:

Step 1:-
Create a developer account in Azure Active Directory with the help of
Microsoft Developer membership domain email. After login successfully, go to the Azure Active Directory overview page.

Step 2:-
The next step is to create your application in Azure.

For that, click Enterprise application from the menu and click new application, choose Non-gallery application then name your application and click add.

Your app is now created, but if you want to access this from Mendix you need to give API permissions and Grant-type client credentials to generate the access token.

Step 3:-
Click API permissions from the menu and give access to both API permission and SharePoint permission.

Once the API selection is complete, Click the Grant Admin consent button to provide Admin access for all APIs.

Summary of required Azure API Permissions
Summary of required Azure API Permissions

Step 4:-
Check your application details, it has Client Id and tenant Id.
to get a client secret value, click client credentials, set the expiry date, and click add. Copy the client’s secret value (And save it somewhere safe).

Your Azure AD setup is completed, let’s implement it in Mendix.
We need an Authorization token to access the SharePoint, we need to generate the Access token using grant type Client credentials.

Now for the Mendix Integration

Step 1:-

Create an app in Mendix Studio Pro, create an entity in a domain model and add attributes shown in the below image. Right-click the entity to generate overview pages. Store all the values in the data.

Step 2:-
Create a microflow to get an access token by calling a REST activity.

HTTP Method POST and Location URL:- https://login.microsoftonline.com/{1}/oauth2/token

Step 3:-
In the request tab pass the Client Id, Client value, username and password.

The resource variable is the hostname of the API which is passed as a parameter to this microflow, Resource will vary based on the API.
For example:
https://graph.microsoft.com/v1.0/sites/{siteid}/lists
here Resource value will be
https://graph.microsoft.com

Step 4:-
Create an import mapping to apply in the response tab to store the values and reuse the generated access token.

Life hack : to get an example of a REST activity’s response payload, go to your console in Studio Pro, click advanced -> set Log levels -> locate REST Consume and REST Publish->set them both to TRACE. (Now it will print the payload to the console when ever that service is triggered, until your log levels are reset)

Use debugger to copy the response then create JSON structure and paste the response. select the JSON structure from the newly created import mapping and select the access token element.

This is what the microflow looks like. Now let’s see where we call this microflow.

This is the entire microflow when the file is uploaded and submitted from the front end, as you can see we are calling the above-mentioned microflow here to generate an access token.

After the Token is generated, we need Site ID and Drive ID to Push the file. Pass the generated access token as an ‘authorization: Bearer ‘+token as a Custom HTTP header in the HTTP Headers tab.
Save all the responses in Import mapping the same way we did for the access token.

Step 5:-
To Get Site ID hit this endpoint in call REST activity

HTTP GET Method
https://graph.microsoft.com/v1.0/sites/****.sharepoint.com:/sites/Mendix?$select=id
star defines your domain name.

Step 6:-
Pass the access token value in Custom HTTP headers

You will receive the response like below,
Response:-

{“@odata.context”: “https://graph.microsoft.com/v1.0/$metadata#sites(id)/$entity",“id”: “****************,********************************* “}

Step 7:-
Save this response in JSON structure and apply import mapping, we need to use the ID value from the response.

Step 8:-

Now to get the Drive ID, add call REST activity in the same microflow. Insert a call REST activity action.

HTTP GET method
https://graph.microsoft.com/v1.0/sites/{1}/drives

Pass the Site ID as a parameter
Save the response and apply an import mapping.

Step 9:-
The next step is to Push the file to SharePoint from the Mendix app.
For this, we need to Call the below API

HTTP PUT method
https://graph.microsoft.com/v1.0/drives/{{drive-id}}/items/root:/{{file-name}}:/content
In HTTP headers tab pass Authorization Bearer token

Step 10:-
Create a new edit page and call the above microflow from the save button

Finally, we can run our app, and test it out!

Upload a file, click the submit save button and check your SharePoint to see your uploaded file.

I hope you enjoyed learning about how to push or upload files to SharePoint from a Mendix app with the help of Azure AD and Microsoft.

I hope this article is fruitful for you, Thank you!

Read more

From the Publisher -

If you enjoyed this article you can find more like it on our Medium page. For great videos and live sessions, you can go to MxLive or our community Youtube page.

For the makers looking to get started, you can sign up for a free account, and get instant access to learning with our Academy.

Interested in getting more involved with our community? You can join us in our Slack community channel or for those who want to be more involved, look into joining one of our Meetups.

--

--

Heena S
Mendix Community

Software Engineer (Mendix Intermediate Developer) @ Indium Software with extensive experience in Java programming and Mendix Application Development.