Add attachments to entities in Dynamics CRM
Attaching documents to entities are easier than ever, in this article we will show you how to add attachments to entities that have the 'Notes & Activities' option enabled as well as attaching documents and data to emails.
First lets take a look at how we can add an attachment to an email.
Attachment data in Dynamics CRM are stored as Base64 encoded strings, therefore once we have the data (in a byte array for example) we need to encode it into a Base64 string.
string filePath = @".\010109_INV-01002-NC157M_XrmLinq.pdf";
byte[] data = File.ReadAllBytes(filePath);
Next we will create the attachment record, before creating the attachment record you will need to create an email record. Example below assumes that you have already created the email record.
ActivityAttachment attachment = new ActivityAttachment
{
ActivityMimeAttachmentId = Guid.NewGuid(),
ActivityId = emailId, // pointer to the email
AttachmentNumber = 1,
Body = Convert.ToBase64String(data),
FileName = Path.GetFileName(filePath),
FileSize = data.Length,
MimeType = "application/pdf",
Subject = "01/01/2009 Invoice"
};
All that's left to do now is to issue the Create command on the CrmDataContext and we are done!, How easy was that?
context.Create<ActivityAttachment>(attachment);
Attaching documents & data to notes
Now lets take a look at attaching notes to any entity. In this example we’ll attach a PDF document to a contact record. Just like the example above; the document data is stored in Dynamics CRM as a Base64 string. There are couple of important properties you need to set inorder for CRM to correctly create this attachment.
- ObjectTypeCode
This property tells CRM which entity to attach the note to. You can use our helper fields to populate this field. - ObjectIdType
This property is the same as above, this is used internally by XrmLinq to correctly populate lookup fields. - IsDocument
Annotation note = new Annotation
{
AnnotationId = Guid.NewGuid(),
ObjectIdType = Contact.Fields.SchemaName,
ObjectTypeCode = Contact.Fields.SchemaName,
ObjectId = contactId,
Subject = "Dynamics CRM 4.0 Certification",
IsDocument = true,
DocumentBody = Convert.ToBase64String(data),
FileName = Path.GetFileName(filePath),
FileSize = data.Length,
MimeType = "application/pdf",
NoteText = "Dynamics CRM 4.0 Certification Requirements",
};
Once you have created an instance of the Annotation class and have populated all the required fields issue the Create command on the CrmDataContext.
context.Create<Annotation>(note);
Featured Customer Example
One of our customers used this method to attach images to products in Dynamics CRM, because we allow you to query the annotation entity it is very simple to retrieve the attach document, decode the Base64 data and display the product image.
var productImage = (from a in context.Annotations
where a.ObjectId == productId &&
a.FileName.Equals("system-product-image.jpg")
select a.DocumentBody).SingleOrDefault();
We hope you have found this article useful, we will be writing more articles like this to show the capabilities of XrmLinq.





Try XrmLinq