This is a very good video on functionality of Queues of CRM 2011 works. This is simply yet clearly explained… though of sharing..
Apr 29, 2013
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.
Now we will see how same thing is achieved using a pdf file. You will realise that encoding method and mimetype are different.
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.
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/
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/
Labels:
C#,
code snippet,
CRM 2011
Subscribe to:
Posts (Atom)