Home > CRM Development, MSCRM General > File Attachment to Notes – Dynamics CRM 4.0

File Attachment to Notes – Dynamics CRM 4.0

In Dynamics CRM 4.0, file attachment to entities is pretty simple as compared to CRM 3.0. UploadFromBase64DataAnnotationRequest and UploadFromBase64DataAnnotationResponse object are both deprecated. Instead a new attribute has been introduces in annotation object with documentbody that accepts string in base64 encoding. Please find the below sample:


Reading file data into Byte array and then converting it to base64. Since I am using in memory object of StringBuilder class so I have just used str.ToString() to return string.


Byte[] data;

 ASCIIEncoding encode = new ASCIIEncoding();

data = encode.GetBytes(str.ToString());

string b64 = Convert.ToBase64String(data);


Next, simply create the annotation entity and set the attributes as below:

annotation note = new annotation();           


note.subject = “Test Export”;

note.filename = “exported.txt”;

note.mimetype = “text/html”;

note.documentbody = b64;


note.objectid = new Lookup();

note.objectid.type = “new_export”;

note.objectid.Value = entityid;

note.objecttypecode = new EntityNameReference();

note.objecttypecode.Value = “new_export”;


Guid annotationId = service.Create(note);


  1. Mayank
    August 6, 2008 at 10:33 am

    Hi, I want to create CRM workflow which updates the case follow up date based on priority. I
    f priority is high –> follow up date would be 24 hrs (1 day) after the case create date.
    If priority is normal –> follow up date would be 72 hrs (1 day) after the case create date
    Now the problem is that CRM includes (or counts) the weekends (Saturday/Sundays) while calculating the days after 3 days. I want to exclude these non business days to be counted in workflow.
    Is there any ways we can avoid saturdays/sundays while dynamically setting date values in CRM 4.0 workflow? Any idea??

  2. Nilesh
    September 19, 2008 at 5:44 am

    This is regarding notes entity.
    Consider a scenario: I have user level access on Company and user level at notes. If another user comes and add a note into my company record.Then logically i should not be allow to update/delete that record. But i can do so.

    Logically speaking it should not happen. Hence the access level permission for notes and attachement are either delete every thing or none.. kindly give me suggestion.

  3. Graham
    October 9, 2008 at 12:21 pm


    Have you any example code of attaching a .PDF , .xlsx or .DOC(X) files as attachments to Email? I have got the .txt files to work but I am having difficulty getting the others to work.


  4. October 10, 2008 at 2:06 am

    Have you changed the mimetype. you need to be very carefull in specifying mimetype.

    note.mimetype = “text/html”;

  5. Graham
    October 10, 2008 at 7:27 am

    I have tried application/msword , application/pdf . For the word doc I see the text but a lot of untranslated chars as well. PDF file just says not enough data to show image. I think it must be something to do with the streamreader and encoding code ?

  6. Colonel87
    December 15, 2008 at 7:23 pm

    I have to send email with attachment created for specific Case. How I can do it?
    Also, how I can save the path of the original attachment before it added to Annotation Table?

  7. wahid
    February 25, 2009 at 2:43 pm

    I want to attach a word document to notes, if i use the above method the formatting of document is lost.

    Is there any way so that i can attach the document alongwith formating.

  8. February 25, 2009 at 8:14 pm

    Hi Wahid,

    you need to set the approperiate mime type for file to be attached. YOu can set the mime type to “application/msword” for Microsoft word documents. You can give a try!

  9. Jaka
    May 27, 2009 at 2:08 pm

    Hi Ayaz,
    Do you know if it’s possible to have a rich text format on Note section? Can we create our own field type and assign the new type to any entity?
    Thanks in advance.

  10. May 27, 2009 at 8:56 pm

    Hi Jaka,
    Its not possible out of the box.

  11. Jaka
    May 27, 2009 at 9:37 pm

    Thanks. But do you know anyone (or ISV) that have developed this stuff? Do you think it’s even possible?

  12. May 27, 2009 at 10:13 pm

    Its very much possible using unsupported ways. People have developed custom controls for CRM. just google.

    Just thinking how can you achieve this. You can even introduce your own tab on the form as Notes and then add iframe and aspx page to enter/read text using rich text box. when submit simply add this text as note to the entity using CRM sdk. So there are endless possibilities. But if you are desinging a complex solution or vertical, i recommend to use supported ways as upgrade will cost a lot in case of unsupported customization.

  13. soren hansen
    May 28, 2009 at 8:36 am

    Another issue…

    I have attatched an xlsx document in CRM 4.0, and it has been created in tabel: annotation.
    Now i would like to make use of this annotation in my code. Need to access a sheet within this file and use the data to update other entities i CRM.

    I have retrievde the annotation by the objectid.
    and decoded the content of documentbody (a string) using Convert.FromBase64String, and have now a byte-array.
    My question is what to do next, in order to re-create the xlsx file in-memory ?
    Any suggestions ?

  14. May 28, 2009 at 9:31 pm

    You need to look at OpenXML that mcirosoft use to play with office 2007 file. Also look at


    It may help you.

  15. Ismail
    August 7, 2009 at 12:54 pm


    I working on small project of integrating with CRM.Basically just creating a Letter activity with doc attachment to Note. Your example will definitely help me. Just wondering if you a complete example. Also I have been struggling with setting Owner for the activity object.
    Please help me, I am very newbie to CRM customisation.

    Ismail Mogal

  16. August 21, 2009 at 12:21 pm

    Thank you for your article!

  17. Miguel
    September 25, 2009 at 11:00 am


    I want to get the file associated with a note in CRM 3.0.
    I know how to get the note, the file name, but not the file contents.

    How do I do this?

  18. October 19, 2009 at 5:26 am

    Use following code to upload file to an annotation.

    FileInfo pointer = new FileInfo( “c:/test.pdf”);
    FileStream fileStream = pointer.OpenRead();
    byte[] byteData = new byte[(int)fileStream.Length];
    fileStream.Read(byteData, 0, (int)fileStream.Length);
    string encodedData = System.Convert.ToBase64String(byteData);


    UploadFromBase64DataAnnotationRequest upload =
    new UploadFromBase64DataAnnotationRequest();
    upload.AnnotationId = newNoteID;
    upload.FileName = “test.pdf”;

    upload.MimeType = “application/pdf”;

    upload.Base64Data = encodedData;

    UploadFromBase64DataAnnotationResponse uploaded =

  19. Eddi Rae Melton
    August 3, 2010 at 6:50 pm

    I am looking for a way to take information from a field on the Lead form and on the OnSave event, create a note and then clear out the field. I see the code above, but I am not sure what to exclude to that is only for the file attachment.
    Eddi Rae

  20. August 9, 2010 at 1:32 am

    Hi Eddi,
    Above code is for file attachements. But in your scenario, you need to write a plugin/workflow to perform this operation.

    For any further assistance, you can email me at ayaz.ahmad@hotmail.com.

    Best Regards,

  21. suyakodyazmak
    March 25, 2011 at 3:11 pm

    Hi ayaz
    I’m new crm programmer.I want to add a picture into the entity What can i do?

  22. August 23, 2011 at 3:20 pm

    I dont understand what I am supposed to do with this.

    Do I create an entity and then create attributes like the ones listed below?

    annotation note = new annotation();

    note.subject = “Test Export”;
    note.filename = “exported.txt”;
    note.mimetype = “text/html”;
    note.documentbody = b64;

    note.objectid = new Lookup();
    note.objectid.type = “new_export”;
    note.objectid.Value = entityid;
    note.objecttypecode = new EntityNameReference();
    note.objecttypecode.Value = “new_export”;

    Guid annotationId = service.Create(note);

    Where do I actually add code on the onload event for the form and what code. Sorry this post makes no sense to me I am not a CRM programmer by any means but I know how to create and customize entities.

  23. August 23, 2011 at 11:54 pm

    This is C# code and you need to write it in Plugin or even you can write it in custom workflow activities.

  24. Dan
    September 25, 2011 at 3:23 am

    Excellent job on this write-up! I truly like how you presented your facts and how you created it interesting and easy to comprehend. Thank you.

  25. jorge abiad
    November 5, 2012 at 1:43 pm

    I need a similar implementation of this in .net 4.0 crm 2011 on-premise. I,ve trie the above code but i got lot of errors.

  26. annonymous
    March 1, 2013 at 5:40 pm

    This blogger has plagiarized your article and is claiming your work as his own:

  27. Anthony
    March 18, 2013 at 12:16 pm

    Byte[] data;
    ASCIIEncoding encode = new ASCIIEncoding();
    data = encode.GetBytes(str.ToString());
    string b64 = Convert.ToBase64String(data);

    You don’t have to encode attachment. Just use attachment.body:

    newAnnotation.documentbody = attahment.body;
    newAnnotation.filesize = attahment.filesize;
    newAnnotation.mimetype = attahment.mimetype;

  28. July 31, 2013 at 3:59 am

    Hey there just wanted to give you a quick heads up. The text in your article
    seem to be running off the screen in Internet explorer. I’m not sure if this is a formatting issue or something to do with web browser compatibility but I thought I’d post to
    let you know. The layout look great though! Hope you get the
    problem fixed soon. Kudos

  29. October 28, 2013 at 12:43 pm

    Have you ever considered about adding a little bit more than just your articles?
    I mean, what you say is important and all. But imagine if you added some great pictures or video clips to give your posts more,
    “pop”! Your content is excellent but with images and video clips, this site
    could definitely be one of the very best in its niche. Excellent blog!

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: