web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

Multiple PO's attachments are not getting downloaded

(0) ShareShare
ReportReport
Posted on by 133

Hi all,

So I created one button on PO, from which we can download PO's attachments using this link.

In above link, it shows how to create Zip file and download the attachment in that zip.

Now, what I want is to download attachments of multiple POs as shown in below image.

4657.attachment-issue.png

As per my code, it runs twice for both the POs which is shown in the info message.

But it downloads only one zip file and it's attachment of the last PO selected.

I debugged the code, it's working fine, no errors. Still I am not able to get both zip files.

What could be the issue? Any suggestions?

Thanks & regards,

Bhagyashree Dave

I have the same question (0)
  • Alex VN Profile Picture
    1,994 on at

    Hi,

    Sorry but for this I think it is better to have some debugging screenshot.  You can focus on the way you get the attachment, download it and zip in in the file and see if any step is missing. May be the attachment is not getting downloaded, or not added in the zip.

    Regards,

  • Bhagyashree Dave Profile Picture
    133 on at

    Hi,

    I used following code to create zip file and download attachments in it. where I have wrote info message right after zip file(with attachment) is sent to user.

    public static void main(PurchTable  _purchTable)
        {
            System.IO.Stream fileStream;
            System.IO.MemoryStream zipArchiveStream = new System.IO.MemoryStream();
            DocuRef docuRef, docuRefLoc;
            DocuValue docuValue, docuValueLoc;
    
            const str extensionZip = '.zip';
            str zipFileName = _purchTable.PurchId;
        
            select firstonly docuvalue
                join docuref
                    where docuref.valuerecid == docuvalue.recid
                    &&    docuref.refrecid == _purchtable.recid
                    &&    docuref.RefTableId == _purchTable.TableId;
    
            if (docuRef.RecId != 0)
            {
                // Creates the zip folder.
                using (System.IO.Compression.ZipArchive zipArchive = new System.IO.Compression.ZipArchive(
                zipArchiveStream,
                System.IO.Compression.ZipArchiveMode::Create,
                true))
                {
                    while select docuRefLoc
                        where docuRefLoc.RefRecId == _purchTable.RecId
                        &&    docuRefLoc.RefTableId == _purchTable.TableId
                    {
                        select docuValueLoc
                            where docuValueLoc.RecId == docuRefLoc.ValueRecId;
    
                        // Creates the file entry in the zip folder.
                        System.IO.Compression.ZipArchiveEntry fileEntry = zipArchive.CreateEntry(docuValueLoc.filename());
                
                        // Opens the created file entry and copies the binary file information of the original file to the file entry in the zip folder.
                        using (System.IO.Stream fileEntryStream = fileEntry.Open())
                        {
                            // Gets the file stream of the document attachment.
                            fileStream = DocumentManagement::getAttachmentStream(docuRefLoc);
                            fileStream.CopyTo(fileEntryStream);
                        }
                    }
                }
    
                // Send a download link of the created zip folder to the user.
                File::SendFileToUser(zipArchiveStream, zipFileName   extensionZip);
                info(strFmt("zip file created for PO %1", zipFileName));
            }
        }

    Note that not only attachments, but even the Zip file is not getting generated for multiple POs. It is only getting generated with attachments for last selected PO only.

    Below I have attached code of on click event of Download button, which works fine as per the info message, and when I debug.

    [FormControlEventHandler(formControlStr(PurchTable, FormButtonControl1), FormControlEventType::Clicked)]
        public static void FormButtonControl1_OnClicked(FormControl sender, FormControlEventArgs e)
        {
            FormDataSource  purchTableDS = sender.formRun().dataSource(formDataSourceStr(PurchTable, PurchTable));
            PurchTable      purchTable = purchTableDS.getFirst(true);
    
            for(purchTable = purchTableDS.getFirst(true); purchTable; purchTable = purchTableDS.getnext())
            {
                BITAttachmentDownload::main(purchTable);
            }
        }

    And here, I have attached image of the output.

    PO-attachment.png

  • Alex VN Profile Picture
    1,994 on at

    Hi,

    Then 1 thing I'm suspect is that this method:

    File::SendFileToUser(zipArchiveStream, zipFileName + extensionZip);

    As it send file to the user, I'm not sure if you make subsequence call multiple time then it will be able to send the file to user.

    I would recommend you to test with getting the download link first for all the attachment, then validate the information or can try to use below method to open the url. With this I think you can narrow down the issue.

    new Browser().navigate("URL");

    Regards,

  • Verified answer
    Martin Dráb Profile Picture
    237,908 Most Valuable Professional on at

    This seems to be related to Download attachments of multiple PO. There I suggested that you can download multiple files by packing them in a single ZIP file. Therefore you would have three orders, each with one attachment, and you would download a single ZIP archive with three files. But what you do is creating three ZIP files, therefore you still have the same problem even when using ZIP.

    Getting multiple files for download is possible (by opening multiple tabs), but if you're using a ZIP archive anyway, I think you should simply use it in the right way.

  • Bhagyashree Dave Profile Picture
    133 on at

    Hi Martin,

    Thanks for your reply.

    I tried your suggestion and now I am able to create one zip with all the attachments in it. But as you said, it will not be possible to create multiple zip files PO wise.

    Thanks again.

  • ML-22040759-0 Profile Picture
    on at

    Hi,

    Can you please send the code to create one zip with all the attachments in it. Above code is for getting one zip file for one po.

    Thanks in advance.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 544 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 450 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 250 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans