Friday, July 16, 2010

Using CRM Advanced Developer Extensions to Add Attachment to Contact

This code sample is straight from MSDN, but it is so often used, I wanted to make sure I kept it handy:

 

// Add a note with a document attachment to the contact's record.

var attachment = File.OpenRead("capture.png");

var data = new byte[attachment.Length];

attachment.Read(data, 0, (int)attachment.Length);

 

var note = new Xrm.annotation()

{

  subject = "Note subject...",

  notetext = "Note Details....",

  Contact_Annotation_id = contact.contactid,

  objecttypecode = "contact",

  documentbody = Convert.ToBase64String(data),

  filename = Path.GetFileName(attachment.Name),

  mimetype = "image/png"

};

crm.AddToannotations(note);

crm.SaveChanges();

 

 

No comments: