.NET · C# · Dynamics CRM 2011

CRM 2011: Upload Attachment/File in Annotation/Note Programmatically

In this post, I’ll provide a quick code snippet to upload attachment/file via CRM 2011 annotation/note.

Inh this example I am using early-bound code generated by crmsvcutil.

var serviceContext = new GeneratedServiceContext();

//We need to create the annotation record first
var annotation = new Annotation();
annotation.Subject = "Attachment Test";
annotation.ObjectId = new EntityReference("account", accountId);
annotation.ObjectTypeCode = "account";
serviceContext.AddObject(annotation);

//Now we get the base64 representation of the file
var textContent = "Hello World!";
byte[] bytes = new byte[textContent.Length * sizeof(char)];
Buffer.BlockCopy(textContent.ToCharArray(), 0, bytes, 0, bytes.Length);
var textContentBase64 = Convert.ToBase64String(bytes);

annotation.FileName = "Plain text file";
annotation.MimeType = @"text/plain";
annotation.DocumentBody = textContentBase64;
serviceContext.UpdateObject(annotation);

serviceContext.SaveChanges();

Cheers – Sy

Advertisements

4 thoughts on “CRM 2011: Upload Attachment/File in Annotation/Note Programmatically

  1. I am soign something like this plus:

    annotaion.OverriddenCreatedOn = CDate(noteItem.sTid).ToUniversalTime()

    annotaion.OwnerId = New EntityReference(“systemuser”, uid.Id)

    but it doesnt add on, so u have any idea why?

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