Wednesday, August 11, 2021

Bulk Export Item Pictures in Business Central SaaS version as Zip File

Hello Experts,

Use the below sample code to bulk export items pictures at a tims from item master.

Make sure you have imported the picture in your Item list or for testing purpose just import the pictures for some items and then check

you can use the below code as it is for your test environment.


    Procedure DownloadItemImage()
    var
        MyItem: Record Item;
        TenantMedia: Record "Tenant Media";
        datacompresion: Codeunit 425;
        blobStorage: Codeunit "Temp Blob";
        PicInStream: InStream;
        ZipInStream: InStream;
        ZipOutStream: OutStream;
        ZipFileName: Text;
        ItemCnt: Integer;
        Index: Integer;
        PicCount: Integer;
    begin
        ZipFileName := 'Picture.zip';
        datacompresion.CreateZipArchive();
        MyItem.Reset();
        MyItem.FindSet();
        repeat
            if MyItem.Picture.Count > 0 then begin
                ItemCnt := ItemCnt + 1;
                for Index := 1 to MyItem.Picture.Count do begin
                    PicCount := PicCount + 1;
                    if TenantMedia.Get(MyItem.Picture.Item(Index)) then begin
                        TenantMedia.calcfields(Content);
                        if TenantMedia.Content.HasValue then begin
                            TenantMedia.Content.CreateInStream(PicInstream);
                            datacompresion.AddEntry(PicInStream, MyItem."No." + '.png');
                        end;
                    end;
                end;
            end;
        until MyItem.Next() 0;
        Message('Items processed ' + Format(ItemCnt' Pictures processed ' + Format(PicCount));
        blobStorage.CreateOutStream(ZipOutStream);
        datacompresion.SaveZipArchive(ZipOutStream);
        datacompresion.CloseZipArchive();
        blobStorage.CreateInStream(ZipInStream);
        DownloadFromStream(ZipInStream, 'Download zip file''''', ZipFileName);
    end;

This code simple create the steam of image media and add in zip archive

at last we download the full image file zip at once using DownloadFromSteam command



I hope it will help someone in future.

Thank you reading this blog.

Keep Sharing....Keep Growing...

No comments:

Post a Comment