Today we will look at how to use SmartConnect to import/export files with NAV/Business Central. These files can be several different types including JPG/PNG/PDF/MP3/MP4, you can find a full list of supported types here. The most common requirement we see is to import image files or attach invoices and receipts. I will show how I use SmartConnect to import and export the Item image in Business Central. These same processes can be used with any other BLOB in NAV/BC with a little modification to the AL code. If you are looking to integrate files with CRM I have a previous help article for how to manipulate notes and attachments. If you are looking to integrate files with SalesForce, then check out this blog post.
*Note that any code provided is for use as a point of reference and will not be supported by eOne Solutions.
Media in Business Central
Before we can begin, we need to understand how Business Central stores media. The actual file is stored in a binary large object (BLOB) field. This field is stored in the Tenant Media table in Business Central. The Media record is then included in a Media Set on the Item record. This will allow multiple media objects to be linked to a single record. To keep this example simple, we will only work with a single image for each item, but the data structure looks like this:
The standard Business Central web services do not expose BLOBs so we will need to publish an Extension to handle this. In today’s post we will go over how to do write an extension for exporting Item Images, Importing Images in Base64, and Importing Images by URL.
I have written a sample extension that includes all three methods outlined below.
Exporting Media
When exporting media from Business Central we needed to convert the file to a format that can be transferred over web services without being corrupted. I am going to do this reading and conversion with a procedure. Note that due to the processing required for each image this will be a slow service, and you may need to extend the time out on the Business Central Connector in SmartConnect.
The first thing I will do is check if there is a picture. If there is no picture I am returning a generic value of ‘No Content’. After validating there is a picture I am creating a stream to the picture and converting the picture to Base64.
You will notice I am dynamically creating a filename and finding the Mime type so SmartConnect doesn’t have to do this, but it could be done in SmartConnect with a calculated column.
I can map the Base64Image field created by this web service directly to a CRM attachment, send it to another web service, or download it locally with a Calculated Column like this:
Dim FilePath as String = "C:UsersAdministratorDesktop" + _FILENAME
Dim imageFile as FileStream = new FileStream(FilePath, FileMode.Create)
Dim Image As Array = System.Convert.FromBase64String(_BASE64IMAGE)
imageFile.Write(Image ,0, image.Length)
imageFile.Flush()
Importing a Base64 Image
Importing an Image requires checking if a picture already exists, if it does, we are telling the service to remove it prior to importing a new one.
Next, we convert the base64 file into a stream so we can load it into our media object. We need to leverage the Temp Blob Codeunit for this, because we can’t read directly from the base64 image to the Picture Media.
We can map any Base64 encoded image to the Base64Image field created on this service. We will also need to tell Business Central what type of file it is with the Mime field. You can find a list of supported file types here. You can get the Base64 image directly from a CRM attachment, retrieve it from another web service, or if you are using SmartConnect On-premise you can upload a local file directly to Business Central with a Calculated Column like this:
return System.Convert.ToBase64String(File.ReadAllBytes(_FILEPATH))
Importing an Image from a URL
There may be times when the picture you want to upload will come from a URL. This is common with eCommerce Platforms like WooCommerce. So, I added one more procedure to handle this.
We can tell Business Central to open a connection to the URL provided, download the image, and save it to the Item Picture media record.
The mapping for this endpoint is simple:
Errors
During my testing I only encountered one error when using the UploadItemPictureUrl procedure.
Failed to create record.: The request was blocked by the runtime to prevent accidental use of production services.
Go to Business Central > Extensions > Item Image Web Service > Configure and enable ‘Allow HttpClient Requests’.
If you have any more questions, feel free to reach out to us at support@eonesolutions.com