Dynamics 365 Business Central: How to add Media or MediaSet data type (Pictures) to a report

Dynamics 365 Business Central

Hi, Readers.
Today I would like to talk about how to add Media or MediaSet data type (Pictures) to a report in Business Central.

As you might know, there are two ways that you can upload media in Business Central:

  • Use a BLOB data typeYou add media to a BLOB data type field on the record. For more information, see BLOB Data Type.

For example, Picture in Company Information:

  • Use a Media or MediaSet data type: This way enables you to store media in system tables of the database, and then reference the media from application records. For example, you can:
    • Display media with records in list type pages, when the page is viewed in the Tile layout. For more information, see Displaying Data as Tiles.
    • Display media on a card type page for a record.
    • Display media in a report.

Customer Picture: Media Data Type

Item Picture: MediaSet Data Type

Using the Media or MediaSet data type provides better performance than using a BLOB data type and is more flexible in its design. With a BLOB data type, each time the media is rendered in the client, it’s retrieved from the SQL database server, which requires extra bandwidth and affects performance. With the Media and MediaSet data types, the client uses media ID to cache the media data, which in turn improves the response time for rendering the media in the user interface. More details: Working With Media on Records

Let’s look at two specific examples.
The object of this development is report 1305 “Standard Sales – Order Conf.” (This standard report contains two types of layouts). This time we use ReportExtension Object, if you are using a version earlier than BC18, please copy the standard report completely. More details: Report extensibility (ReportExtension Object)

1. When printing Standard Sales – Order Conf., we need to print out the customer pitcure.

Imported media is stored as an object in the system table 2000000184 Tenant Media of the tenant database. Each media object is assigned a unique identifier (ID).

Source Code:

Word Layout:

RDLC Layout:

Test Video:

2. We also need to print item pictures in sales lines.

A media set is an ordered list of media objects, determined by the order in which the media objects were added to the media set. This order can’t be changed. To identify this order, each media object is assigned an index number, starting a 1. The first media added gets the index 1, the second media gets the index 2, and so on. If a media object is removed from the set, the list is reindexed. More details: Indexing of media objects in a media set

If a MediaSet data type field is used in a report object, then only the first associated media file is displayed in the generated report.

Source Code:

Word Layout:

RDLC Layout:

Test Video:

Very simple, give it a try!!!😁

Source Code: Github (Please note that the source code is for reference only, you can improve it according to your own needs)

reportextension 50111 ZYStandardSalesOrderConfExt extends "Standard Sales - Order Conf."
{
    dataset
    {
        add(Header)
        {
            column(CustPicture; CustTenantMedia.Content)
            {
            }
        }
        add(Line)
        {
            column(ItemPicture; ItemTenantMedia.Content)
            {
            }
        }
        modify(Header)
        {
            trigger OnAfterAfterGetRecord()
            var
                Cust: Record Customer;
            begin
                if Cust.Get("Sell-to Customer No.") then
                    if Cust.Image.HasValue then begin
                        CustTenantMedia.Get(Cust.Image.MediaId);
                        CustTenantMedia.CalcFields(Content);
                    end;
            end;
        }
        modify(Line)
        {
            trigger OnAfterAfterGetRecord()
            var
                Item: Record Item;
            begin
                if Type = Type::Item then
                    if Item.Get("No.") then
                        if Item.Picture.Count > 0 then begin
                            ItemTenantMedia.Get(Item.Picture.Item(1));
                            ItemTenantMedia.CalcFields(Content);
                        end;
            end;
        }
    }

    var
        CustTenantMedia: Record "Tenant Media";
        ItemTenantMedia: Record "Tenant Media";
}

END

Hope this will help.

Thanks for reading.

ZHU

コメント

Copied title and URL