Media, such as images are frequently used in Dynamics 365 Business Central. To work with media files, you need to learn about file handling. This blog post introduces Media, Mediaset data types, and how to upload and download images using MediaSet in Dynamics Business Central 365.
What is a Media, Mediaset?
Media data type holds a record with a single media object. MediaSet data type can hold more than one media object. All media objects are stored in the system table 2000000184 Tenant Media and each media object are assigned a unique identifier GUID. If a media file is added to MediaSet then the object referenced in the system table 2000000183 Tenant Media Set. Since Media and MediaSet are data types that store media in system tables of the database using a Media or MediaSet is more efficient than using the Blob data type.
Creating Media Table and Page
First, we need to create a table with a MediaSet field. Use this Media table as a source table for a List Page and add a factbox on a page.
The Media Factbox page is a CardPart page that displays the Picture on the MediaSet field and has 2 actions: import and export.
This action imports a media file into the MediaSet field. The first part of the code tests if the field “No.” is not empty otherwise an error message is displayed. The second part of the code counts the number of images the MediaSet field holds. If it has any images, the dialog box appears with a warning for overwriting. After that, we are using streaming functions to select and upload a media file. Function UploadIntoStream is used to upload a media file to the InStream. Now, we clear the image field and get a picture from InStream with the function ImportSteam and the picture is displayed on the page.
The second action exports the image from the field. We are using for loop to get all images stored in the MediaSet field. When a media object is inserted in a MediaSet, it is assigned an index number by the order in which the media objects were added to the media set, that is why each image can be accessed by Rec.Image.Item(index). It retrieves the GUID of the media object in the MediaSet field. Function CalcFields is used to load the contents of the field from the database into memory. To download files, we are using the DownloadFromStream function.
Viewing Tenant Media Table
As told previously, all imported images are stored in Tenant Media Table. We can check uploaded media files on the Table information Page. Open the Business Central web client and open the Table Information page. On this page write Media in the search bar to find the Tenant Media table.
Click “No. of Records” to open a list of all media files stored in Tenant media.
Similar to BLOBs, the Media data type is used to store an image related to various records, but unlike the BLOB datatype, which can store only 1 image, the Media object can be included in a MediaSet, which allows the storage of multiple images. Understanding how to use MediaSets can allow the developer to customize the solution to provide more visual information about their records.