Implementing usage of files from local computer paths with Persistent Blob functionality in Dynamics 365 Business Central

Guidelines for Partners

Most Dynamics NAV and Business Central 365 solutions use file management functionality to work with external files (read, write, save, download files, etc.) However, when using C/AL, usually most of the file management functions are based on the file path from the local computer since the client’s local file system is available, unlike in Business Central (in Web Client, it is possible to work only with the file system on the server side). This article describes examples of the Persistent Blob functionality implementation, which refactors the usage of files from local computer paths in Business Central.

 

The “Persistent Blob” table allows storing different types of files as Blob data in Business central since SaaS model does not have access to the local file system. This article will cover two ways Persistent Blob functionality can be implemented – using SystemApp’s standard objects or creating your own custom “Persistent Blob” table (Figure 1). Since Standard 4151 “Persistent Blob” table is an Internal System Table and not directly accessible, it can only be used by codeunit’s 4101 “Persistent Blob” functions (the situation is the same as with standard “Temp Blob” functionality).

 

 

The idea behind the Persistent Blob functionality is that the link to the file is not a local path but a key to the “Persistent Blob” record, by which the required file is found. The key must be stored in the selected table/tables to be later used to find the file you are looking for.

 

1. Persistent Blob functions: SaveAsPdf, SaveAsExcel, SaveAsXML, SaveAsWord, SaveAsHTML

 

The local path and name of the file you want to save is a parameter of functions SaveAsPdf, SaveAsExcel, SaveAsXML, SaveAsWord, SaveAsHTML. The examples below show how to refactor the function SaveAsPdf by using the function SaveAs, which allows saving reports as a PDF, Excel, Word, HTML, or XML file. Function SaveAs is described in more detail here.

  • Persistent Blob implementation using custom-created table:

 

 

  • Persistent Blob implementation using standard objects (“Temp Blob” codeunit is used because “Persistent Blob” codeunit does not have functions CreateOutSream and CreateInStream):

 

Temp Blob

 

“Persistent Blob” table’s “Primary Key” usage

 

Below is an example of how a file is found and used based on the file’s Blob data key assigned to the record. Firstly, created file’s “Primary Key” is assigned to a record. Later, when a file is needed, it is retrieved from the “Persistent Blob” table based on the Blob data key assigned to the record.

 

Primary Key

 

Added custom field “FileBlobKey” to table “Sales Header” saves key to record’s file. Custom action “Download Blank Sales Order Report” downloads report if Sales Order’s record “Sales Header” has attached file through key, which is stored in the custom field “FileBlobKey”.

  • Sales Order’s custom action if Persistent Blob is implemented using custom created table:

 

FileBlobKey

 

  • Sales Order’s custom action if Persistent Blob is implemented using standard objects:

 

Persistent Blob

 

2. XMLPort Export function

 

This example is intended for cases when the XMLPort‘s Export function was used after setting a local path where the file should be saved. The Xmlport.The export function used in the workaround is described in more detail here.

  • Persistent Blob implementation using custom-created table:

 

XMLPort Export function

 

  • Persistent Blob implementation using standard “Persistent Blob” codeunit:

 

“Persistent Blob” codeunit

 

3. XMLPort Import function

 

This example is intended for XMLPort‘s Import function after opening a file from a local path. Since the local file system is inaccessible, the required file is found not by its local path but by “Primary Key“ linked to the file’s Blob data. “Primary Key“ is stored in the variable “FileKey“ – the Xmlport. The import function used in the workaround is described in more detail here.

  • Persistent Blob implementation using custom-created table:

 

Persistent Blob

 

  • Persistent Blob implementation using standard “Persistent Blob” codeunit:

 

Persistent Blob

 

4. XMLDocument.Load function and File.Exists/Exists function

 

Xml document’s Load function loads the .xml document based on the local path, which is provided as the function’s parameter. Often, before using the XMLDocument.Load function, the File.Exists (or Exists) function is executed to check whether the required file exists based on the local file path provided as a parameter to the function. The examples below show the workaround of described two functions. XMLDocument.The load function is described in more detail here.

  • Persistent Blob implementation using custom-created table:

 

Persistent Blob

 

  • Persistent Blob implementation using standard “Persistent Blob” codeunit:

 

Persistent Blob

 

5. XMLDocument.Save function

 

Xml document’s Save function saves the .xml document based on the local path provided as the function’s parameter. The examples below show XMLDocument.Save function’s workaround with Persistent Blob functionality. XMLDocument.The save function is described in more detail here.

  • Persistent Blob implementation using custom-created table:

 

Persistent Blob

 

  • Persistent Blob implementation using standard “Persistent Blob” codeunit:

 

Persistent Blob

 

 

Conclusion

 

Business Central Web Client cannot access the client‘s local file system. In this article, we described a solution to handle files in SaaS: Persistent Blob functionality, which allows storing the different types of files as Blob data. Persistent Blob functionality can be implemented using the standard System Application‘s codeunit “Persistent Blob” or by creating a custom “Persistent blob” table if access is needed directly. The main idea behind the Persistent Blob functionality is to save a file in the “Persistent Blob” table as Blob data by assigning it a later key to finding the stored file (the key replaces the local file path).