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, ...
Suggested Answer

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

(0) ShareShare
ReportReport
Posted on by 962

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
    237,770 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
    962 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
    237,770 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

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 692 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

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

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 364 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans