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.

We are going to implement following two steps,
  • 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,

  1. <html><head>  
  2. <script type="text/javascript">  
  3. //check if document is loaded or not  
  4. var imgControl = document.createElement("IMG");  
  5. //Check if documented loaded fully  
  6. document.onreadystatechange = function () {  
  7.    if (document.readyState == "complete") {  
  8.       getnotesImages();  
  9.    }  
  10. }  
  11.    
  12. //this function is used to get image from notes  
  13. function getnotesImages()  
  14. {  
  15.  //get regarding object id  
  16.    var regardingObjectId=window.parent.Xrm.Page.data.entity.getId().substring(1, 37);  
  17.    
  18.    //prepare URL  
  19.    var webapiURL=window.parent.Xrm.Page.context.getClientUrl() + "/api/data/v8.2/annotations";   
  20.    
  21.    //prepare query  
  22.    var webapiQuery ="?$select=annotationid,documentbody,mimetype&$filter=_objectid_value eq "+regardingObjectId+" and  isdocument eq true and startswith(mimetype, 'image/')";  
  23.    
  24. //call image  
  25. var req = new XMLHttpRequest();  
  26. req.open("GET", webapiURL+webapiQuery, true);  
  27. req.setRequestHeader("OData-MaxVersion", "4.0");  
  28. req.setRequestHeader("OData-Version", "4.0");  
  29. req.setRequestHeader("Accept", "application/json");  
  30. req.setRequestHeader("Content-Type", "application/json; charset=utf-8");  
  31. req.setRequestHeader("Prefer", "odata.include-annotations=\"*\",odata.maxpagesize=1");  
  32. req.onreadystatechange = function() {  
  33.     if (this.readyState === 4) {  
  34.         req.onreadystatechange = null;  
  35.         if (this.status === 200) {  
  36.             var results = JSON.parse(this.response);  
  37.            if(results.value.length>0) {  
  38.                 var annotationid = results.value[0]["annotationid"];  
  39.                 var documentbody = results.value[0]["documentbody"];  
  40.                 var mimetype = results.value[0]["mimetype"];  
  41.                 //set image  
  42.                 imgControl.src="data:" + mimetype + ";base64," + documentbody;  
  43.                 document.getElementById('imagediv').appendChild(imgControl);  
  44.             }  
  45.         } else {  
  46.             Xrm.Utility.alertDialog('Error while retrieving image details');  
  47.         }  
  48.     }  
  49. };  
  50. req.send();  
  51. }  
  52. </script>  
  53. <head><body style="zoom: 1; word-wrap: break-word;">  
  54.    
  55. <div style="width: 100px;" id="imagediv"></div>  
  56.    
  57. </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.

Hope it will help someone!!


Similar Articles
HIMBAP
We are expert in Microsoft Power Platform.