Hi all,
I am trying to create a pdf attachment for a custom entity using the following code:
Entity entity = context.InputParameters["Target"] as Entity;
string content = entity["xyz_notes"].ToString();
byte[] byteArray = Encoding.UTF8.GetBytes(content);
Entity annotation = new Entity("annotation");
annotation.Attributes["subject"] = "Syllabus";
annotation.Attributes["notetext"] = "Syllabus";
EntityReference noteRef = new EntityReference();
noteRef.LogicalName = "xyz_syllabusnotes";
noteRef.Id = entity.Id;
annotation["documentbody"] = Convert.ToBase64String(byteArray);
annotation["filename"] = "Syllabus.pdf";
annotation["mimetype"] = @"application/pdf";
annotation.Attributes.Add("objectid", noteRef);
annotation.Attributes.Add("objecttypecode", "sjs_syllabusnotes");
_service.Create(annotation);
The code creates a pdf as an attachment in the record, but when i try to download and open the PDF, it says that it is Corrupted!
I know that PDFs are in binary format, but the annotation["documentbody"] attribute does not accept the byte array unless i convert it to Base64 string.
Please Help!
Thanks,
Kautuk
*This post is locked for comments