Dynamics 365 Business Central: How to import files from a zip file (Bulk import attachements)

Dynamics 365 Business Central

Hi, Readers.
A few days ago, I was asked a intersting question, is there any way to bulk import attachments for master data in Business Central. For example, for customer master.

Yes, it’s possible, but we can’t do it with standard feature, such as Configuration Package. We need to do a small customization.

Last year we briefly discussed how to add multiple files into a zip file and download it. More details: https://yzhums.com/20030/
Today, I would like to talk about how to import files from a zip file. Both of these features are valid for SaaS.

In fact, there is a very good standard feature for reference, Import Item Pictures. You can find out more about Import Multiple Item Pictures from MS Docs.

You can import multiple item pictures in one go. 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.

Source code you can refer to:

page 348 “Import Item Pictures”:

table 31 “Item Picture Buffer”:

Okay, next let me do a simple sample.

First add an Import button to the customer list.

Then add import logic: Like Import Item Pictures feature, simply name your files with names corresponding to your customer numbers.

And when the customer does not exist, an error will be prompted.

Test Video:

Source Code: GitHub: ImportZipFile

pageextension 80100 CustomerListExt extends "Customer List"
        addafter("Sent Emails")
                Caption = 'Import Zip File';
                ApplicationArea = All;
                Promoted = true;
                PromotedCategory = Process;
                PromotedIsBig = true;
                Image = Import;
                ToolTip = 'Import Attachments from Zip';

                trigger OnAction()

    local procedure ImportAttachmentsFromZip()
        FileMgt: Codeunit "File Management";
        DataCompression: Codeunit "Data Compression";
        TempBlob: Codeunit "Temp Blob";
        EntryList: List of [Text];
        EntryListKey: Text;
        ZipFileName: Text;
        FileName: Text;
        FileExtension: Text;
        InStream: InStream;
        EntryOutStream: OutStream;
        EntryInStream: InStream;
        Length: Integer;
        SelectZIPFileMsg: Label 'Select ZIP File';
        FileCount: Integer;
        Cust: Record Customer;
        DocAttach: Record "Document Attachment";
        NoCustError: Label 'Customer %1 does not exist.';
        ImportedMsg: Label '%1 attachments Imported successfully.';
        //Upload zip file
        if not UploadIntoStream(SelectZIPFileMsg, '', 'Zip Files|*.zip', ZipFileName, InStream) then

        //Extract zip file and store files to list type
        DataCompression.OpenZipArchive(InStream, false);

        FileCount := 0;

        //Loop files from the list type
        foreach EntryListKey in EntryList do begin
            FileName := CopyStr(FileMgt.GetFileNameWithoutExtension(EntryListKey), 1, MaxStrLen(FileName));
            FileExtension := CopyStr(FileMgt.GetExtension(EntryListKey), 1, MaxStrLen(FileExtension));
            DataCompression.ExtractEntry(EntryListKey, EntryOutStream, Length);

            //Import each file where you want
            if not Cust.Get(FileName) then
                Error(NoCustError, FileName);
            DocAttach.Validate("Table ID", Database::Customer);
            DocAttach.Validate("No.", FileName);
            DocAttach.Validate("File Name", FileName);
            DocAttach.Validate("File Extension", FileExtension);
            DocAttach."Document Reference ID".ImportStream(EntryInStream, FileName);
            FileCount += 1;

        //Close the zip file

        if FileCount > 0 then
            Message(ImportedMsg, FileCount);


Hope this will help.

Thanks for reading.