Authorization, Base64 Convert, Business Central, InStream, Integration, JSON, JsonObject, JsonToken, OData, Post, Postman, SOAP, Tenant Media, Tip & Tricks, URL, Web Access Key, Web Services

Integrating with Business Central – using Web Service

This is fourth post in this series, if you wish to see first post, you can access here.

In second post I discussed about, how to create Azure Functions, you can access here.

In previous post I discussed about, how to Integrate Azure Functions in Business Central, you can access here.

In this post we will discuss how to make available QR Image using API by exposing as Web Service.

Let’s cut the theoretical part and coming directly to the solution.

This post is very short and as an answer to query from one of the blog readers, who regularly gives feedback and suggest topic he wish from me. Sometime it is not possible to write post for every query, but I try to pick some which I find interesting. Converting any topic into post is quite time taking, solution can be of 10 mins but presenting same in form of blog takes 2-3 hours, sometimes even more.

Some time appropriate environment is not available where I can put the solution and take screenshots to present as blog.

Since this topic is part of current series, I am writing posts on, so I was able to easily pick this topic.

Requirement is to add capability to get QR Image we generated in our previous posts using Web Service/ API/ OData/ JSON Base64Text.

To get this working we will first create a Codeunit with appropriate methods and expose it as Web Service.

Step – 1.

Create Codeunit & Add below method.

Step – 2

Publish above Codeunit as Web Service.

Step – 3

Copy SOAP URL

[SOAP URL]

https://api.businesscentral.dynamics.com/v2.0/1e4fc386-962e-4a20-8364-d5aeb65e2485/Test/WS/CRONUS%20USA%2C%20Inc./Codeunit/QRCodeGenerator

Convert to ODataV4 URL by making necessary modification as below:

[ODataV4 URL]

https://api.businesscentral.dynamics.com/v2.0/1e4fc386-962e-4a20-8364-d5aeb65e2485/Test/ODataV4/QRCodeGenerator

Similarly, your $metadata URL will be:

https://api.businesscentral.dynamics.com/v2.0/1e4fc386-962e-4a20-8364-d5aeb65e2485/Test/ODataV4/$metadata

Run the metadata URL in browser:

Search for your function created above, you should get definition as below:

<Action Name=”QRCodeGenerator_GetQRSetImageAsJSON”>

<Parameter Name=”qRSetId” Type=”Edm.Int32″/>

<ReturnType Type=”Edm.String”/>

</Action>

Let’s test the above URL for function using Postman to see the result:

[POST] – https://api.businesscentral.dynamics.com/v2.0/1e4fc386-962e-4a20-8364-d5aeb65e2485/Test/ODataV4/QRCodeGenerator_GetQRSetImageAsJSON?Company=CRONUS USA, Inc.

Authorization:  Basic Auth

Username: ASHWINI.TRIPATHI

Password: xjAtAtTaFhgXkgR4M5WoHapotvJNmqJ5//XBYK2v5s4= (Web Service Access Key)

JSON Parameter to Function: (Body raw:JSON)

{

    “qRSetId” : “6”

}

Result Body:

{

    “@odata.context”: “https://api.businesscentral.dynamics.com/v2.0/1e4fc386-962e-4a20-8364-d5aeb65e2485/Test/ODataV4/$metadata#Edm.String&#8221;,

    “value”: “iVBORw0KGgoAAAANSUhEUgAAApQAAAKUAQMAAACAATp/AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAGUExURQAAAP///6XZn90AAAAJcEhZcwAADsMAAA7DAcdvqGQAAAHtSURBVHja7dpbasMwEAXQ2YH3v0vvwKVBsZ44DZ4UWs58BEnMHH1eYjuO9NqZTCaTyWQymUwmk8lkMplMJpPJZDKZ/8KMsbb27Ls3St+2bmYymUwmM8Ws58N2b+FiXjQzmUwmk3nTLMN1qkPq6LqZyWQymczPmUdTwxVMJpPJZP6G+di0/7lqH5PJZDKZnzOnK7ZzW2tbNzOZTCaTmWIOtbXw9DM3M5lMJpOZYK6rPvSL9huJywkmk8lkMu+Yz0d9tR7HQ7INl9UWJpPJZDITzDIwv39arVbNTCaTyWRmmS0XbZ5Nq64uM47JZDKZzDfMdmrv4W47ZByTyWQymalm7S2rre0tq+eNxRw4JpPJZDITzDhjq8uu9p5udSLBZDKZTGae+UyxKd4GqWbc3MxkMplMZpZ5EXSdVPuYTCaTyUw12/kh1Dq4rVcZx2QymUzmG+aqinmcU12etdceTCaTyWSmmDHWkHaDNJ8xmUwmk5li1vM6GjFfu60nmEwmk8nMMuvUFGV7zPWjjGMymUwm87ZZt/Pqcc/wXJDJZDKZzE+apSXOV1TRf9HHZDKZTGaiub4i+ijb+48iDiaTyWQyM82h2rZYRFmsYpDJZDKZzHtmVjGZTCaTyWQymUwmk8lkMplMJpPJZDL/rHnsXwW1n/zFqtyZAAAAAElFTkSuQmCC”

}

Copy the Value and use any base64 – image converter tool, search on Google

You will get your QR Image:

Don’t try any URL or Passwords in this post as same is modified, and in images covered to hide actual url & passwords.

Hope you enjoyed learning from this post.

That’s all for this post, but visit again to learn from upcoming posts in this series, will post soon.

See you again in next post, till then keep exploring, learning and sharing with others.

Advertisement
AL, Business Central, Development Tips, File, How To, Information, InStream, Modern Development Tool, OutStream, Tip & Tricks, V2, Visual Studio Code, XMLports

File Handling using Stream – Business Central

Today in this post we will discuss about using Stream to Export or Import Files or in other words File handling using Streams.

First Example I am discussing about is how to Import Image to Item Picture.

First part of the code checks for if any existing Picture in the records Picture field, here we use Count to check, because Picture field is defined as MediaSet can hold multiple picture, so giving warning for Overwrite.

In Second part we are Uploading the Selected Image to the InStream.

Finally we clear the Picture field and load Picture from InStream, here file name will be used as description for the image.  And save the record post importing of the Image.

Below code sequence is self explanatory.

StreamImpImage

In Second example we are Exporting the image from the record, any/all images stored in Picture field of the Item record.

Again we are testing with Count if any Image stored, else flag the Error nothing to Export as record don’t have any picture stored.

As told above since it is MediasSet type so possibility of more than one Image may have been stored, so we setup for loop to access all of them.

In TenantMedia table all Images are stored with unique GUID, so we get them each by Item(index).

Calcfields used to load the content in memory and final checking if it HASVALUE.

We build the filename as Item_Image1.ext, we will look into GetImgFileExt function below.

Next we load the Image in InStream.

Finally we save the Image on disk by specified filename.

StreamExpImg

This below function we use to find right extension of the file from TenantMedia field Mime Type.

StreamGetFileExt

In last and below function we are using TempBlob Table and Stream to handle file.

We create the OutStream of BlobTable Blob Field, set it as destination for my Export XML Port.

Further creating the InStream we are transferring the content of Blob to the File.

StreamExportContact

Hope you will start using this technique in your future projects, when working with File Handling.

We will discuss on other concepts in our upcoming posts.

Not sure if this is my Last post for this year, so wishing you Happy New Year 2019 in advance.

Keep practicing and learning, see you again in my next post.

Till then bye, take good care of your self and remain safe.