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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
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 972

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

I have the same question (0)
  • Suggested answer
    Martin Dráb Profile Picture
    238,751 Most Valuable Professional on at

    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().

  • JustZM Profile Picture
    972 on at

    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

  • Martin Dráb Profile Picture
    238,751 Most Valuable Professional on at

    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.

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

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

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 510 Super User 2026 Season 1

#2
Giorgio Bonacorsi Profile Picture

Giorgio Bonacorsi 399

#3
Adis Profile Picture

Adis 251 Super User 2026 Season 1

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans