Spread the love

First off, I will start with a disclaimer, I am not a Power Automate guru, but I thought that it was about time that I spend some time to learn the ropes, and then I thought why not share my journey with you, so we can learn together 😊  

What is Power Automate and why should I care? 

Well, unless you are totally new to the world of Microsoft development you have properly heard about Microsoft Flow, well Power Automate is Microsoft Flow just rebranded to fit into their new Power Platform 😎 . When we talk about Power Automate we talk about automating different workflows, and the power of Power Automate comes in its simplicity💪 , because you can do pretty much anything with a few clicks and by using some of their many build-in templates. One of the many strengths is its easy integration into anything Microsoft, in only using a few clicks you can have a webservice up and running that will let you place a file on a SharePoint, one drive or FTP server.  So, the reason why you should care about Power Automate is that it will make your life a whole lot easier, and your customers much happier since everything can be done in no time🤩 . 

From Business Central to SharePoint 

In this first post we will take a look at how we can easily take any file and place it on a SharePoint site, now I will show you how you can do this from Business Central, but you can call the REST webservice from any programming or scripting language. 

First thing you have to do is go to https://flow.microsoft.com and create an account or sing in with you existing Microsoft Account. Then go to create. 

And here you have the ability to choose either some build in flows, or to create your own, we will go with an Instant flow. 

Next choose the trigger called When an Http request is received 

Click the trigger and create your json body and choose Post: 

{ 

    "type": "object", 
    "properties": { 
        "ApiToken": { 
            "type": "string" 
        }, 
        "FileName": { 
            "type": "string" 
        }, 
        "Document": { 
            "type": "string" 
        } 
    } 
} 

In my pay load I will take en API Token for security, the filename and Document which will be my Base64 encoded string. 

Next click New Step and choose Control, and then condition 

Next we will control that our token is correct, so choose ApiToken and then a strong token. 

Now if this webservice is called with the correct token you should upload the file to SharePoint so in the If Yes box choose add action. 

And find the create file. 

Power Automate will then use your user to find which SharePoint sites you have access to, choose one and a folder in file name choose FileName. 

In File content go to Expression and write base64ToBinary(triggerBody()?[‘Document’]) 

So, it should look something like this: 

And that is it you now have a REST service created in Power Automate to place a file on your SharePoint, now give you flow a name and save it.  

Here you might be meet with some billing restrictions, unfortunately Power Automate is not free 🙁 However once you have choose you plan you can start testing your flow with Postman. 

You can then call the webservice with something like this: 

And you Power Automate will then create your file on your SharePoint destination.  

Next you will have to create the rest webservice call from Business central, which could be done with the following code: 

procedure SendToFlow(FileName: Text; Base64: Text) 

    Var 
        client: HttpClient; 
        cont: HttpContent; 
        header: HttpHeaders; 
        response: HttpResponseMessage; 
        Jobject: JsonObject; 
        tmpString: Text; 
    Begin 
        Jobject.Add('ApiToken', ‘{token}’); 
        Jobject.Add('Document', Base64); 
        Jobject.Add('FileName', FileName); 
        Jobject.WriteTo(tmpString); 
        cont.WriteFrom(tmpString); 
        cont.ReadAs(tmpString); 
        cont.GetHeaders(header); 
        header.Remove('Content-Type'); 
        header.Add('Content-Type', 'application/json'); 

        client.Post(‘{ flow url}’, cont, response); 

    end; 

And we are done, so something that would normally take I do not know how many hours to do the old fashion way 😏, can now be completed in 30 minutes of coding 🤯 , that was it for this time, stay tuned as I try to dig more into what we can do with Power Automate, and until next time stay safe. 😷

Leave a Reply