Apr 2, 2013

Creating notes programmatically

Adding notes for any data record is a powerful feature in Microsoft Dynamics CRM 2011. In this way, user can merge external information as files in different formats such as text, pdf, image or etc.


All these files are kept in CRM in Note entity. Schema name for this entity is annotation and it is not highly customizable. In fact, there will not be a need of doing it. However we might need to create and copy annotations. You can even retrieve data.

If you check the DB, you will realise how those notes are kept in the table fields.


For me, most exciting field is documentbody, that keeps the content as encoded data. Objectid is the other remarkable field, which is actually the lookup for related record. Now we will see a couple of code snippets;

This is how we can create an annotation using a text file. In my sample I am doing it for a specific record in quote. Record is mentioned by Guid.

string filePath = @"C:\Sumedha\log\Sumedha.txt";
byte[] fileContent = File.ReadAllBytes(filePath);
string encodedData = System.Convert.ToBase64String(fileContent);

Entity _annotation = new Entity("annotation");
_annotation.Attributes["objectid"] = new EntityReference("quote", new Guid("41613500-9F87-E211-905C-00155D467A0E"));
_annotation.Attributes["objecttypecode"] = "quote";
_annotation.Attributes["subject"] = "Demo";
_annotation.Attributes["documentbody"] = encodedData;
_annotation.Attributes["mimetype"] = @"text/plain";
_annotation.Attributes["notetext"] = "My Sample attachment";
_annotation.Attributes["filename"] = "MySample.txt";
service.Create(_annotation);

Now we will see how same thing is achieved using a pdf file. You will realise that encoding method and mimetype are different.

FileStream _stream = File.OpenRead(@"C:\Sumedha\log\TestPDF.pdf");
byte[] _bData = new byte[_stream.Length];
_stream.Read(_bData, 0, _bData.Length);
_stream.Close();
string encodedData = System.Convert.ToBase64String(_bData);

Entity _annotation = new Entity("annotation");
_annotation.Attributes["objectid"] = new EntityReference("quote", new Guid("41613500-9F87-E211-905C-00155D467A0E"));
_annotation.Attributes["objecttypecode"] = "quote";
_annotation.Attributes["subject"] = "Demo";
_annotation.Attributes["documentbody"] = encodedData;
_annotation.Attributes["mimetype"] = @"application/pdf";
_annotation.Attributes["notetext"] = "My Sample attachment";
_annotation.Attributes["filename"] = "MySample.pdf";
service.Create(_annotation);

Also you can do the same without a file, if you are interested in using just a string as the input. For that you can do the data encoding as below and do the rest as usual.

string _str ="Sample - file doesnt get created in server";
byte[] _bstr = Encoding.ASCII.GetBytes(_str);
string encodedData = System.Convert.ToBase64String(_bstr);

By the way, I found a nice blog article that provides a lot of code snippets in this regards. Please refer it here;
http://lakshmanindian.wordpress.com/2012/11/01/attachments-in-microsoft-dynamics-crm-2011/

5 comments:

  1. Thankyou, found this useful in my CRM 2015 installation. If you are using a FileUpload control, here a few things I used:

    byte[] fileContent = FileUpload1.FileBytes;

    string contentType = FileUpload1.PostedFile.ContentType;
    _annotation.Attributes["mimetype"] = contentType;

    _annotation.Attributes["filename"] = FileUpload1.FileName;



    ReplyDelete
  2. Thank you James, yep this code would be helpful to me as well as for other readers. Cheers.

    ReplyDelete
  3. Hey Guys ..m using Crm2016 online version and have to save pdf file in record as notes if record is update.I'm able to save word file but not able to save pdf ...file is getting saved as PDF but not opening ...Please help.

    ReplyDelete
  4. Probably you want to attach PDF as a Attachment ..isn't it ?

    ReplyDelete