Introduction:
As we all know Microsoft has introduced many of new client APIs in Dynamics 365 v9.0 which makes dynamics CRM developer’s life easy. ‘CaptureImage’ is one of the useful API for Dynamics 365 mobile users.
In this blog, we will see how this API helps Dynamics 365 mobile users to capture image and use it in Dynamics CRM.
Note: This API invokes the device camera to capture an image and supported only for the mobile clients.
To understand, we will use this API and will attach captured image in notes of account entity record on save of form.
Syntax of the API:
Xrm.Device.captureImage(imageOptions).then(successCallback, errorCallback)
Where imageOptions which are an optional parameter where we can define below attributes in this Object:
- allowEdit: This is a Boolean value to indicate whether to edit image before saving it.
- Height: This is a Number value to define height of the image to capture.
- preferFrontCamera: This is a Boolean value to indicate whether to use front camera of the device to capture image.
- quality: This is a number value to define quality of the image file in percentage.
- width: We can also define width of the image to capture in Number value.
successCallback:
This function is required and we can call once the image is captured. It will return a base64 encoded image object with the following attributes:
- fileContent: In this attribute, we will get contents of the image file in string.
- fileName: This will give name of the image file in string.
- fileSize: This will give size of the image file in KB in number.
- mimeType: This will give image file MIME type in string.
errorCallback:
This function is also required and we can call this when the operation fails.
Above is the syntax in which we can write our function.
We have written the function and call this on save of the Account entity form.
function captureImage() { var functionName = "captureImage"; try { var imageOptions = {}; imageOptions.allowEdit = true; Xrm.Device.captureImage(imageOptions).then(function (data) { var annotation = {}; annotation["objectid_account@odata.bind"] = "/accounts(" + Xrm.Page.data.entity.getId() + ")"; annotation["subject"] = "Demo Image"; annotation["notetext"] = "Demo"; annotation["documentbody"] = data.fileContent; annotation["filename"] = data.fileName; annotation["mimetype"] = data.mimeType; //create notes Inogic.Maplytics.ApiLib.create("annotations", annotation, function () { alert("Image Attached Successfully."); }, function (error) { alert("Error:>> " + error.message) }); }, function (error) { throwError(error.message); }) } catch (e) { throwError(functionName, e); } }
Once this function gets called, the Image will get attached in notes of the record form. We can also use this API to change the account entity record image in Dynamics CRM.
Conclusion:
Using ‘CaptureImage’ API Dynamics 365 mobile users can capture image and use it in Dynamics 365 CRM.