Dynamics 365 Business Central: How to export all item pictures – Customization

Dynamics 365 Business Central

Hi, Readers.
Today I would like to talk about a question I was asked recently, how to export all item pictures.

As you might know, you can import multiple item pictures in one go in Business Central. Simply name your picture files with names corresponding to your item numbers, compress them to a zip file, and then use the Import Item Pictures page to manage which item pictures to import. More details: Import Multiple Item Pictures

Business Central short video: Import Multiple Item Pictures

But what if you want to export all the item pictures? As of now, there is no standard function to do this, and you can only export them one by one.

So we can only consider customization. For example, save the pictures into a zip file, then download it.
We have discussed similar requirements before, and this time is no exception. You can also refer to previous practices.

Let’s simulate this requirement.
Add two actions, one to export all pictures, and one to export pictures of selected items.

Test video:

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

pageextension 50100 ItemListExt extends "Item List"
{
    actions
    {
        addafter(AdjustInventory)
        {
            action(ExportAllItemPictures)
            {
                Caption = 'Export All Item Pictures';
                ApplicationArea = All;
                Promoted = true;
                PromotedCategory = Process;
                PromotedIsBig = true;
                Image = Export;

                trigger OnAction()
                var
                    Item: Record Item;
                begin
                    Item.Reset();
                    ExportItemPictures(Item);
                end;
            }
            action(ExportSelectedItemPictures)
            {
                Caption = 'Export Selected Item Pictures';
                ApplicationArea = All;
                Promoted = true;
                PromotedCategory = Process;
                PromotedIsBig = true;
                Image = Export;

                trigger OnAction()
                var
                    Item: Record Item;
                begin
                    Item.Reset();
                    CurrPage.SetSelectionFilter(Item);
                    if not Item.IsEmpty then
                        ExportItemPictures(Item);
                end;
            }
        }
    }

    local procedure ExportItemPictures(var Item: Record Item)
    var
        TempBlob: Codeunit "Temp Blob";
        ItemPictureInStream: InStream;
        ZipOutStream: OutStream;
        ZipInStream: InStream;
        DataCompression: Codeunit "Data Compression";
        ZipFileName: Text[50];
        ItemTenantMedia: Record "Tenant Media";
        FileName: Text;
        FileExtension: List of [Text];
    begin
        ZipFileName := 'ItemPictures_' + Format(CurrentDateTime) + '.zip';
        DataCompression.CreateZipArchive();
        if Item.FindSet() then
            repeat
                if Item.Picture.Count > 0 then begin
                    ItemTenantMedia.Get(Item.Picture.Item(1));
                    ItemTenantMedia.CalcFields(Content);
                    ItemTenantMedia.Content.CreateInStream(ItemPictureInStream, TextEncoding::UTF8);
                    FileExtension := ItemTenantMedia."Mime Type".Split('/');
                    FileName := Item."No." + '.' + FileExtension.Get(2);
                    DataCompression.AddEntry(ItemPictureInStream, FileName);
                end;
            until Item.Next() = 0;
        TempBlob.CreateOutStream(ZipOutStream);
        DataCompression.SaveZipArchive(ZipOutStream);
        TempBlob.CreateInStream(ZipInStream);
        DownloadFromStream(ZipInStream, '', '', '', ZipFileName);
    end;
}

Give it a try!!!😁

PS: Dynamics 365 Business Central: Import multiple item pictures at the same time without ZIP file (fileuploadaction, AllowMultipleFiles property, and FileUpload data type) – Customization | Dynamics 365 Lab

END

Hope this will help.

Thanks for reading.

ZHU

コメント

Copied title and URL