D365 Business Central : Batch Export All Item Pictures to a Zip File

Item Pictures

If you want to export Item Picture into a file, you can use below code:

DownloadFromStream(FileInStream, ”, ”, ”, FileName); 

However, if you try to export multiple pictures, BC will only export the last picture. This is an issue which I hope Microsoft will address in the future.

The workaround is to compress it into a zip file using Data Compression codeunit.

Below is a sample on how to export all item pictures into a zip file.

local procedure ExportAllItemPictures()
    var
        TenantMedia: Record "Tenant Media";
        Item: Record "Item";
        DataCompression: Codeunit "Data Compression";
        TempBlob: Codeunit "Temp Blob";
        iLoop, MaxLoop : Integer;
        PictureInstream, ZipFileInstream : Instream;
        ZipFileOutstream: OutStream;
        ItemFileName, ZipFileName : Text;
        FileCount: Integer;
    begin
        Item.Reset();
        If Not Item.FindSet() then
            exit;

        ZipFileName := 'ItemPictures.Zip';
        FileCount := 0;
        DataCompression.CreateZipArchive();

        repeat
            MaxLoop := Item.Picture.Count();
            If MaxLoop <> 0 then begin
                for iLoop := 1 to MaxLoop do begin
                    if TenantMedia.Get(Item.Picture.Item(iLoop)) then begin
                        TenantMedia.Calcfields(Content);
                        if TenantMedia.Content.HasValue() then begin
                            TenantMedia.Content.CreateInStream(PictureInstream);
                            ItemFileName := Item."No." + GetTenantMediaFileExtension(TenantMedia);
                            DataCompression.AddEntry(PictureInstream, ItemFileName);
                            FileCount += 1;
                        end;
                    end;
                end;
            end;
        until Item.Next() = 0;
        If FileCount > 0 then begin
            TempBlob.CreateOutStream(ZipFileOutstream);
            DataCompression.SaveZipArchive(ZipFileOutstream);
            TempBlob.CreateInStream(ZipFileInstream);
            DownloadFromStream(ZipFileInstream, '', '', '', ZipFileName);
        end;
        DataCompression.CloseZipArchive();
    end;

local procedure GetTenantMediaFileExtension(TenantMedia: Record "Tenant Media"): Text;
    begin
        case TenantMedia."Mime Type" of
            'image/jpeg':
                exit('.jpg');
            'image/png':
                exit('.png');
            'image/bmp':
                exit('.bmp');
            else
                Error('Unsupported Mime Type %1', TenantMedia."Mime Type");
        end;
    end;

thatnavguy

Experienced NZ-based NAV Developer and Consultant with 15+ years of experience leading multiple IT projects, performing business analyst, developing, implementing, and upgrading Dynamics NAV and Business Central. Passionate to deliver solution that focuses on user-friendly interface while keeping high standard of compliance with the needs.

You may also like...

1 Response

  1. 21 July 2021

    […] Batch Export All Item Pictures to a Zip File […]

Leave a Reply

Your email address will not be published. Required fields are marked *