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 :
Microsoft Dynamics CRM (Archived)

Adding a Note\Annotation attachment as an Email Attachment

(0) ShareShare
ReportReport
Posted on by

I've been trying for several hours to get this to work to no avail.  

I have a custom entity with notes enabled.  I manually added note to one of my records with a PDF attachment.  The purpose of this is that another plugin will use this attachment to include within a custom e-mail when needed.  So the only thing I need to do is pull the note with attachment and transfer the attachment to the email attachment.

I can do this numerous ways that work with no errors (the email and attachment get created correctly), however when I download the attachment from the e-mail it says the PDF is damaged.

Here is what I have tried so far, the first and simplest approach is what I think should work:

1:

myActivityMimeAttachment["body"] = myNote.GetAttributeValue<AliasedValue>("note.documentbody").Value;

2:

myActivityMimeAttachment["body"] = (string)myNote.GetAttributeValue<AliasedValue>("note.documentbody").Value;

3:

// Converting back to byte array and to filestring and back to base 64 under different formats
string stringBase64 = (string)myNote.GetAttributeValue<AliasedValue>("note.documentbody").Value;
byte[] byteFile = Convert.FromBase64String(stringBase64);
string fileString = Encoding.Unicode.GetString(byteFile); 
myActivityMimeAttachment["body"] = Convert.ToBase64String(new UnicodeEncoding().GetBytes(fileString)); // Error - damaged


I tried several different encoding types in the above example....nothing works.

Any help is appreciated!

Coty

*This post is locked for comments

I have the same question (0)
  • Community Member Profile Picture
    on at

    Just a small update:  

    I tried just reading the PDF from the file system and saving to an email attachment.  

    Like this:

                byte[] bytes = System.IO.File.ReadAllBytes("Notes.pdf");
                myActivityMimeAttachment["body"] = Convert.ToBase64String(bytes);


    This works fine and the file is not corrupted/damaged, it's only copying the field from a note attachment that damages the file.

    thanks,

    Coty

  • Kokulan Profile Picture
    18,054 on at

    Hi

    You do not need to convert the document body again, you can set that as is because its already in Base64 string. And you have to set the Mime type for it be recongnised as a PDF file.

    Please try the following

    myActivityMimeAttachment["body"] = myNote.GetAttributeValue<AliasedValue>("note.documentbody").Value;

    myActivityMimeAttachment["mimetype"] = myNote.GetAttributeValue<AliasedValue>("note.mimetype").Value;  - Make sure you include both document body and mimetype columns when you query to note to get the attachment.

  • Community Member Profile Picture
    on at

    Hi Kokulan,

    Thanks for the response.  I have tried setting the mimetype exactly as you have above already (and tried hard coding to application\pdf and it does not work.

    I just noticed something that doesn't look correct though.  In the e-mail attachment it has a file size of 1500.  That does not seem correct.  My e-mail has 2 attachments, one is a PDF and one is a small txt file.

    4804720.Capture.PNG

    That seems too small, Does CRM truncate the base64 string for any reason?

    Coty

  • Community Member Profile Picture
    on at

    I found the issue - not sure how to fix it yet...

    myNote.GetAttributeValue<AliasedValue>("note.documentbody").Value;


    Is only returning the first 1500 characters for some reason.   I verified in the DB it is ~11,000 characters.

  • Verified answer
    Community Member Profile Picture
    on at

    I found the answer:

    community.dynamics.com/.../crm-issue-with-retrieving-large-document-body-using-fetch-xml-solution

    I had to remove distinct="true" from my fetch XML query.......

    Really that was it.  I had everything else correct.  My entire day wasted.  Thank you CRM.

  • Suggested answer
    Pawar Pravin  Profile Picture
    5,237 on at

    Hi Codiax,

    Please refer following code, its working for me....

    string _subject = nt.Attributes.Contains("subject") ? nt.GetAttributeValue<string>("subject") : "";

    string _filename = nt.Attributes.Contains("filename") ? nt.GetAttributeValue<string>("filename") : "";

    Entity note = new Entity("annotation");

    note["subject"] = _subject;

    note["filename"] = _filename;

    note["documentbody"] = nt.Attributes["documentbody"];

    if (emailguid != Guid.Empty)

    {

    note["objectid"] = new EntityReference("email", emailguid);

    }

    Guid attachmentId = service.Create(note);

    Entity note1 = service.Retrieve("annotation", attachmentId, new ColumnSet(true));

    Entity attachment = new Entity("activitymimeattachment");

    attachment["subject"] = _subject;

    string fileName = _filename;

    attachment["filename"] = _filename;

    byte[] fileStream = new byte[] { };

    attachment["body"] = Convert.ToBase64String(fileStream);

    attachment["mimetype"] = "text / plain";

    attachment["attachmentnumber"] = 1;

    attachment["objectid"] = new EntityReference("email", emailguid);

    attachment["objecttypecode"] = "email";

    service.Create(attachment);

  • RaviKashyap Profile Picture
    55,410 Moderator on at

    Thanks Codiax for posting your finding. This will surely help someone help facing the same issue. :)

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 > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans