Skip to main content

Notifications

Finance | Project Operations, Human Resources, ...
Suggested answer

How could I download File from SP and attach file to Email?

(0) ShareShare
ReportReport
Posted on by 960

Hi All

I setup a (SharePoint) document type in Dynamics. When user create new Purchase Order, user will add file(s) to attachments (SharePoint).

New we need a customization, to send email to vendor with attachment(s). So, we need code to download file(s) from SP attachment and send email to Vendor.

However, when I use below code

"url = DocumentManagement::getAttachmentPublicUrl(docuRef)"

The URL is too long because it is URL+token. when I use this URL, I can navigate to.

new Browser().navigate(url, DocuSharedParameters::isOpenAttachmentsInNewWindowEnabled(), false); // success

but I will get error in

_client.DownloadFile(url,docuValue.FileName + '.' + docuValue.FileType); // Failed because string is to long.

I also tried this one

JfileStream = DocumentManagement::getAttachmentStream(docuRef);

but when I set JfileStream in email parameters, I will get error again.

messageBuilder.addAttachment(JfileStream, docuValue.FileName + '.' + docuValue.FileType);

pastedimage1683049979645v2.png

pastedimage1683050133084v3.png

pastedimage1683049921037v1.png

Does anyone has idea about how to download file from SP (attachment) and send with email?

Best Regards,

JustZM

  • Martin Dráb Profile Picture
    Martin Dráb 230,842 Most Valuable Professional on at
    RE: How could I download File from SP and attach file to Email?

    Yes, of course that getAttachmentAsContainer() uses getAttachmentStream(). I didn't told you to use getAttachmentAsContainer(), I told you to copy the result of getAttachmentStream() to a MemoryStream and that you can see such an example of such code in getAttachmentAsContainer(). Please try my actual suggestion.

    Your code makes no sense to me. It seems that you're creating a new memory stream for every byte, and throw away all except of the last one. Then you attach just the last byte as the file, which obviously isn't the full content of the file.

  • JustZM Profile Picture
    JustZM 960 on at
    RE: How could I download File from SP and attach file to Email?

    Hi Martin

    Thank you for your reply. I just tried getAttachmentAsContainer(), and I can receive email with attachment now. However, getAttachmentAsContainer also use getAttachmentStream function.pastedimage1683053422709v1.png

    I use below function to prepare Stream type

    pastedimage1683053472300v2.png

    when I receive file, and try to open it. I got below error.

    pastedimage1683053636184v3.png

    Please advise.

    Best Regards,

    JustZM

  • Suggested answer
    Martin Dráb Profile Picture
    Martin Dráb 230,842 Most Valuable Professional on at
    RE: How could I download File from SP and attach file to Email?

    A solution for the message builder would be copying the contents to a memory stream and using this stream instead. You can see an example in DocumentManagement::getAttachmentAsContainer().

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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Announcing Forum Attachment Improvements!

We're excited to announce that attachments for replies in forums and improved…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,969 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 230,842 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans