https://www.c-sharpcorner.com/article/creating-html-web-resource-to-show-image-attached-in-notes-using-web-api/
Some time back, I wrote a post for retrieving an image attached to notes using OData, as now for Dynamics 365 CE, we use Web API. So, I am going to share how we can do it using Web API.
Microsoft Dynamics CRM stores all the notes and attachments in annotation entities. Most out of the box entities used to have a relationship with this entity, but while creating our custom entity we can specifically select if we want to associate our custom entity with notes or not use the following option under Communication & Collaboration. Keep in mind once you enable this option, there is no way to disable this option, but if you are not sure, if you need notes or not at the time of your entity creation it's better to leave this option un-selected, so that you can select this option after sometime if required.
Note
The default size for notes attachment is 5 MB but if required, you can increase this limit to up to 32 MB.
- Get entity image from notes
- Create and deploy html web resource
Get entity image from notes
Similar to earlier versions we can upload the image to notes in Dynamics 365 CE.
Once image is attached to notes, we can retrieve it using Web API retrievemultiple request where we can query notes based on ObjectId field, we need to pass entity id field for objectid field in notes. We can using following code, in our html webresource,
- <html><head>
- <script type="text/javascript">
- //check if document is loaded or not
- var imgControl = document.createElement("IMG");
- //Check if documented loaded fully
- document.onreadystatechange = function () {
- if (document.readyState == "complete") {
- getnotesImages();
- }
- }
- //this function is used to get image from notes
- function getnotesImages()
- {
- //get regarding object id
- var regardingObjectId=window.parent.Xrm.Page.data.entity.getId().substring(1, 37);
- //prepare URL
- var webapiURL=window.parent.Xrm.Page.context.getClientUrl() + "/api/data/v8.2/annotations";
- //prepare query
- var webapiQuery ="?$select=annotationid,documentbody,mimetype&$filter=_objectid_value eq "+regardingObjectId+" and isdocument eq true and startswith(mimetype, 'image/')";
- //call image
- var req = new XMLHttpRequest();
- req.open("GET", webapiURL+webapiQuery, true);
- req.setRequestHeader("OData-MaxVersion", "4.0");
- req.setRequestHeader("OData-Version", "4.0");
- req.setRequestHeader("Accept", "application/json");
- req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
- req.setRequestHeader("Prefer", "odata.include-annotations=\"*\",odata.maxpagesize=1");
- req.onreadystatechange = function() {
- if (this.readyState === 4) {
- req.onreadystatechange = null;
- if (this.status === 200) {
- var results = JSON.parse(this.response);
- if(results.value.length>0) {
- var annotationid = results.value[0]["annotationid"];
- var documentbody = results.value[0]["documentbody"];
- var mimetype = results.value[0]["mimetype"];
- //set image
- imgControl.src="data:" + mimetype + ";base64," + documentbody;
- document.getElementById('imagediv').appendChild(imgControl);
- }
- } else {
- Xrm.Utility.alertDialog('Error while retrieving image details');
- }
- }
- };
- req.send();
- }
- </script>
- <head><body style="zoom: 1; word-wrap: break-word;">
- <div style="width: 100px;" id="imagediv"></
div > - </body></html>
Create and deploy html web resource
Now, we need to create an HTML web resource and use the above code under Text Editor.
After that, we can put this web resource to our required Entity Form and we should be able to see the first attached image in web resource after refreshing the page.
Next we will explain how to create a HTML web resource and use our JavaScript method to get and display the image. We need to implement the following two steps:
· Create HTML page
· Deploy HTML page and SDK.REST.js using web resource
Create HTML page
Create a HTML page using a HTML editor. Use the following code for the HTML page:
- <html lang="en-us"><head>
- //Reference clientglobalcontest.js to get context information
- <script src="../ClientGlobalContext.js.aspx"></script>
- //add SDK.Rest.js reference
- <script type="text/javascript" src="SDK.REST.js"></script>
- <script type="text/javascript">
- //check if document is loaded or not
- var imgControl = document.createElement("IMG");
- //Check if documented loaded fully
- document.onreadystatechange = function () {
- if (document.readyState == "complete") {
- getnotesImages();
- }
- }
- //this function is used to get image from notes
- function getnotesImages()
- { //get regarding object id
- var regardingObjectId=window.parent.Xrm.Page.data.entity.getId();
- //assign notes entity name
- var entitySchemaName="Annotation";
- var odataQuery = "?$top=1&$select=AnnotationId,DocumentBody,MimeType&" +
- "$filter=ObjectId/Id eq guid'" + regardingObjectId +
- "' and IsDocument eq true and startswith(MimeType,'image/') ";
- //call retrieveMultipleRecords method in SDK.REST javascript script library
- SDK.REST.retrieveMultipleRecords(entitySchemaName, odataQuery, getnotesImagesCallback, function (error) { alert(error.message); }, function(){});
- }
- //process callbanck result
- function getnotesImagesCallback(resultSet)
- {
- if (resultSet.length > 0) {
- var mimeType = resultSet[0].MimeType;
- var body = resultSet[0].DocumentBody;
- imgControl.src="data:" + mimeType + ";base64," + body;
- document.getElementById('imagediv').appendChild(imgControl);
- }
- }
- </script>
- <meta charset="utf-8"></head><body style="zoom: 1;">
- <div style="width: 100px;" id="imagediv"></div>
- </body></html>
Deploy HTML page and SDK.REST library using web resource
Use following procedure to deploy a HTML page and SDK.REST library in Microsoft CRM.
- Navigate to Settings -> Customization-> Customize the System from the top navigation bar
- Navigate to Components -> Web Resources-> New
- Fill in the details as in the following screen:
- Click on the browse button and select your HTML web resource.
- Click on Save and then Publish
- Navigate to Components -> Web Resources-> New
- Fill in the details as in the following screen:
- Click on browse and select SDK.REST.js from Microsoft CRM SDK
Note: Please refer to my previous article for the SDK.REST.js location.
Now we can place our HTML web resource in the account entity form by navigating to Insert -> Web Resource options. Save your changes and then publish the account entity form.
No comments:
Post a Comment