User Review
( votes)captureImage method of Xrm.Device allows us to take the picture through the camera and work with that captured image in the code (in this post, we are saving the image in the notes as an attachment).
This method is only available for mobile apps.
Here we have added a custom ribbon button on the Lead entity form.
- Capture and attach
The capture Image method will allow the user to capture an image using the device camera and attach it to the notes as shown below.
Clicking on “Capture and attach” ribbon button, opens the camera for the user to take the pic as shown below.
Clicking on OK will ask the user to choose the image editor.
Users can edit the image and click on done.
The dialog informs the user that images have been attached.
Users can refresh the timeline to see the attached image in notes.
If we try using the captureImage method inside the Dynamics 365’s web application, we get the below error, because it is only supported for mobile devices.
The method will throw the below error in case of Web Application
“Operation {0} is not supported by platform”
The sample code for captureImage
function CaptureImage(entityId) { entityId = entityId.replace(/[{}]/g,''); var imageOptions = {}; imageOptions.allowEdit = true; Xrm.Device.captureImage(imageOptions).then( function (data) { // attach the captured image as attachment to note var entity = {}; entity.subject = "Sample Subject"; entity.documentbody = data.fileContent; entity.filename = data.fileName; entity.mimetype = data.mimeType; entity.notetext = "Sample Text"; // lead entity sample entity["objectid_lead@odata.bind"] = "/leads(" + entityId + ")"; Xrm.WebApi.online.createRecord("annotation", entity).then( function success(result) { var newEntityId = result.id; var alertStrings = { confirmButtonLabel: "OK", text: "Image successfully attached to note. Please refersh the timeline" }; var alertOptions = { height: 200, width: 300 }; Xrm.Navigation.openAlertDialog(alertStrings, alertOptions); }, function (error) { Xrm.Utility.alertDialog("Error occured while attaching image to notes. Please try again"); } ); }, function (error) { Xrm.Utility.alertDialog("Error occured while capturing image. Please try again"); } ); }
Add Crm Parameter to pass the GUID of the record to the method
Thus, using captureImage makes it easy for the end-user to capture and attach the image to notes through a click of a button.
To update an entity’s image, we can click on the image itself which will open up the dialog to either use the camera, upload image from the gallery, or revert it to the default image (in not default).
Also check out the wonderful tool Notes Metadata Manager , which provides additional capabilities like adding metadata to the attachments, organise attachments using folder structure, drag and drop attachments, upload multiple attachments etc.
Hope it helps..