Create Attachment of Signature/ Pen control data in Dynamics CRM
Introduction
We already saw how to use Pen/ Signature control in Dynamics CRM in the previous blog: https://www.cloudfronts.com/adding-signature-control-mobile-tablets-dynamics-crm/
In this blog, we will see how to generate an image of the pen control data and store in Record as an attachment.
Steps
- As we saw in the previous blog, the signature captured from phone is stored as a multi-line text field in Dynamics CRM record. And we cannot see the signature image in web browser.
Now it will be a common scenario where the Users will need to see the Signature image in browser as well. This is how data is captured on Web browser.
- We need to write custom code which will:
- Read this data
- Convert the data to an image
- Store the converted image as an attachment in the CRM record
- For this I, have written a generic plugin which will do the above actions.
- You can see the core plugin code with comments below. I have also added the plugin to Github for reference: Github Link
//// The plugin is registered on the Post Update on "Customer Approval" field on Opportunity. Entity entity = (Entity)context.InputParameters["Target"]; //// The field which stores the data for Signature string signatureFieldName = "new_customerapproval"; if (entity.Contains(signatureFieldName)) { string encodedData = entity.GetAttributeValue<string>(signatureFieldName); //// Remove the additional Metadata from the text generated. int startIndex = encodedData.IndexOf("base64,") + 7; encodedData = encodedData.Substring(startIndex, encodedData.Length - startIndex); tracer.Trace(encodedData); string contentType = "image/png"; Entity Annotation = new Entity("annotation"); Annotation.Attributes["objectid"] = new EntityReference(entity.LogicalName, entity.Id); Annotation.Attributes["objecttypecode"] = entity.LogicalName; Annotation.Attributes["subject"] = "Customer Signature"; //// You can have any subject as required. Annotation.Attributes["documentbody"] = encodedData; Annotation.Attributes["mimetype"] = contentType; Annotation.Attributes["notetext"] = "Customer Signature Attached"; //// Again, add any note text as needed Annotation.Attributes["filename"] = "Customer Approval Signature.png"; //// OR Any name as required Guid annotation = service.Create(Annotation); }
- This plugin should be registered on update of the Signature control field. In this case, it is “Customer Approval”. It is preferable to have the step run asynchronously.
- We can now see the attachment on the record.
If you have any issues or need more information, please post in the comments section below.