Importing Annotation (Note) Attachment

The logic conclusion of Exporting Annotation (Note) Attachment. Here is how to programtically create an annotation with an attachment.

void AttachDocument(ICrmService service, Guid entityId, String entityType, String path, String mimeType)
{
String fileName = Path.GetFileName(path); //load the attachment file from disk

annotation a = new annotation(); //we have to create an annotation

a.objectid = new Lookup(entityType, entityId); //and attach to a record, e.g. contact
a.objecttypecode = new EntityNameReference(entityType);

a.subject = fileName;

a.filename = fileName; //the annotation has fields which contain the attachment information
a.mimetype = mimeType;
a.documentbody = Convert.ToBase64String(File.ReadAllBytes(path)); //crm like us to store attachments as base64 strings

service.Create(a);
}
Advertisements

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 )

Google+ photo

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

Connecting to %s