Technical Fun

CRM 4.0: Adding Attachment / Notes Programmatically to Entities

Posted on: June 10, 2008

Holla, CRM Party people, to entertain your greed towards CRM snippets and to keep me from forgetting such a trivial thing, here we go … the adding attachment / note / annotation snippet for CRM 4.0. (In this sample I am binding it to the incident entity).

1 public static bool AddAttachment(string filename, Guid incidentguid, byte[] modal, out string errormessage) 2 { 3 try 4 { 5 string encodedData = Convert.ToBase64String(modal); 6 7 annotation ano = new annotation(); 8 ano.filename = filename; 9 ano.isdocument = new Petrosea.CRM.SD.MetaUtility.CRMWebService.CrmBoolean(); 10 ano.isdocument.Value = true; 11 ano.documentbody = encodedData; 12 ano.mimetype = "application/octet-stream"; 13 ano.objectid = new Lookup(); 14 ano.objectid.type = EntityName.incident.ToString(); 15 ano.objectid.Value = incidentguid; 16 ano.objecttypecode = new EntityNameReference(); 17 ano.objecttypecode.Value = EntityName.incident.ToString(); 18 19 TargetCreateAnnotation target = new TargetCreateAnnotation(); 20 target.Annotation = ano; 21 CreateRequest createrequest = new CreateRequest(); 22 createrequest.Target = target; 23 CreateResponse created = (CreateResponse)crmService.Execute(createrequest); 24 25 errormessage = String.Empty; 26 return true; 27 } 28 catch (Exception ex) 29 { 30 errormessage = ex.Message; 31 return false; 32 } 33 }


5 Responses to "CRM 4.0: Adding Attachment / Notes Programmatically to Entities"

Great post! But what is the purpose of the byte array named “modal” ??

Can you give an example?

dude, it’s supposed to be the file that you’re attaching.

so … roughly ..

FileStream f = new FileStream(WHATEVAFILE,FileModeblabla,FileAccessBlabla);
byte[] f_in_byte_array = new byte[f.length];
blah blah blah ..

and you can pass f_in_byte_array to that method above …

hope it helps

Don’t you dude me! Gehehe šŸ˜‰

I just figured out that the byte array was indeed the file that I wanted to attach… Okay, it was early, didn’t had my coffee yet… yadayada.

Would be wonderful if CRM did the file handling with just the file name though :-S

Anyway : thanx šŸ™‚

First let me say I’m sorry, my question is probably something that is very simple to do but I am new to CRM and can’t find any posts on it.

How can I just add text to a note field, I’m not worried about adding an attachment, I just want to add the text that is typed in a normal text field to the note field.


Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: