Send File via REST call in mendix

1
Hi Everyone ,  I am working on an application where we are trying to send a File document generated in Mendix only to share Point with the help of REST call.  POST https://{site_url}/_api/web/GetFolderByServerRelativeUrl('/Folder Name')/Files/add(url='a.txt',overwrite=true)  Shown above is the URL needs to be created in order to add a file on share Point . I am not clear that how can i gave the path of my file (Saved in document entity) In the  (url='a.txt',overwrite=true)  url section as shown here . Can someone help me how can i send a file from Mendix to share Point . 
asked
2 answers
2

Hi Shivam,

I have implemented this a couple of weeks ago for a client.

I do not know if you need some form of authentication and the steps that I wrote down below are probably very specific for my setup but it may help you get in the right direction. Here are the steps that I have implemented:

  1. You have to create an 'Add-in’ for your sharepoint site. This is so you can authorize the API for connecting to the sharepoint environment. 
     
  2. After you have created the Add-in you can implement a REST POST call that will generate an OAUTH token. The location that I used is: https://accounts.accesscontrol.windows.net/<TenantID>/tokens/OAuth/2 TenantID is a variable and will be generated by the add-in you have created in step 1.

    The header for this REST call is: Key: Content-Type and Value: 'application/x-www-form-urlencoded'. 

    The request for the REST call is: 'grant_type%09='+urlEncode($GrandType)+'%09&client_id='+urlEncode($ClientID_TenantID)+'&client_secret='+urlEncode($ClientSecret)+'&resource='+urlEncode($Resource). Note that you have to combine the ClientID and TenantID into one variable first; hence the $ClientID_TenantID variable

    In the response you will get an AccessToken
     
  3. Now you have to get the context of sharepoint and you do this by a REST POST call to this endpoint: https://<SiteURL>/sites/<SiteName>/_api/contextinfo Of course SiteURL and SiteName are the variables here.

    The header for this REST call are:
    Key: Authorization and Value: 'Bearer ' + $Access_token
    Key: Accept and Value: 'application/json;odata=verbose'

    No request needed for this call

    In the response you will get a FormDigestValue
     
  4. The last call is uploading the file to the sharepoint itself. You need to do a REST POST call to the following location: https://<SiteURL>/sites/<SiteName>/_api/web/lists/GetByTitle('<ListName>')/<FolderPath>/Files/add(url='<FileName>',overwrite=true) All the values between <> are variables of course. 

    The headers for this call are:
    Key: Authorization and Value: 'Bearer ' + $Access_token
    Key: X-RequestDigest and Value: $FormDigestValue

    For the request select: 'Binary for the entire request’. Put the contents of a filedocument here. In my case this was an Excel file.
     

Maybe I forgot something to mention so feel free to ask questions. 

Some extra documentation:

https://docs.microsoft.com/en-us/sharepoint/dev/sp-add-ins/register-sharepoint-add-ins
https://docs.microsoft.com/en-us/sharepoint/dev/sp-add-ins/get-to-know-the-sharepoint-rest-service?tabs=csom
https://medium.com/@yash_agarwal2/performing-oauth-and-rest-calls-with-sharepoint-online-without-creating-an-add-in-677e15c8d6ab

answered
1

I am not sure on share point side, but you could encode you’re file document on Mendix on base64 and send it then by Rest Service in the message. Maybe you take a look in the Module Community Commons for some helpful Java actions.

answered