Image Gallery Utility For Dynamics CRM

This article is about image gallery utility for Dynamics CRM.

In our earlier article we have demonstrated how we can fetch images attached to notes. Working on the same requirement we have enhanced our examples to develop an image gallery utility to see all the images attached to notes. Here is the complete code for this utility:

  1. <!--This is a Image Gallery utility, which can be used with any entity which have    
  2. relationship with Notes, this web resource will show all the images attached to notes -->< html >< head >< style type = "text/css" >    
  3.     
  4.     .left - div {    
  5.         display: inline - block;    
  6.         cursor: pointer;    
  7.     }    
  8.     .right - div {    
  9.         cursor: pointer;    
  10.         display: inline - block;    
  11.     } < /style>< meta charset = "utf-8" >< /head>  
  12. <body style="-ms-zoom: 1;">< script src = "../../ClientGlobalContext.js.aspx" >< /script>< script src = "../Script/SDK.REST.js" type = "text/javascript" >< /script>< script type = "text/javascript" >    
  13. //variables    
  14. var currentImage = 0;    
  15. var img = document.createElement("IMG");    
  16. var images = new Array;    
  17. var TotalImages = 0;    
  18. var SectionName;    
  19. var TabName;    
  20. document.onreadystatechange = function() {    
  21.         if (document.readyState == "complete") {    
  22.             //get query parameters    
  23.             var queryParam = GetGlobalContext().getQueryStringParameters().data;    
  24.             var fields = queryParam.split(",");    
  25.             TabName = fields[0];    
  26.             SectionName = fields[1];    
  27.             //hide control in case form create    
  28.             if (isformtypeCreate())    
  29.                 showImageGalery(TabName, SectionName, false)    
  30.             else {    
  31.                 showImageGalery(TabName, SectionName, true)    
  32.                 getnotesImages();    
  33.             }    
  34.         }    
  35.     }    
  36.     //check for form create mode    
  37. function isformtypeCreate() {    
  38.     if (window.parent.Xrm.Page.ui.getFormType() == 1) //create mode    
  39.         return true;    
  40.     else    
  41.         return false;    
  42. }    
  43. //hide/show image gallery    
  44. function showImageGalery(tabname, sectionname, flag) {    
  45.     window.parent.Xrm.Page.ui.tabs.get(tabname).sections.get(sectionname).setVisible(flag);    
  46. }    
  47. //get images from notes    
  48. function getnotesImages() {    
  49.     var regardingObjectId = window.parent.Xrm.Page.data.entity.getId();    
  50.     var entitySchemaName = "Annotation";    
  51.     var odataQuery = "?$select=AnnotationId,DocumentBody,MimeType&" +    
  52.         "$filter=ObjectId/Id eq guid'" + regardingObjectId +    
  53.         "' and IsDocument eq true and startswith(MimeType,'image/') ";    
  54.     if (typeof(SDK) != "undefined") {    
  55.           SDK.REST.retrieveMultipleRecords(entitySchemaName, odataQuery, getnotesImagesCallback, 
                function(error) {    
  56.             alert(error.message);    
  57.         }, function() {});    
  58.     } else {    
  59.         alert("REST.SDK library not available");    
  60.     }    
  61. }    
  62. //callback method    
  63. function getnotesImagesCallback(resultSet) {    
  64.     if (resultSet.length > 0) {    
  65.         TotalImages = resultSet.length;    
  66.         for (i = 0; i < resultSet.length; i++) {    
  67.             var mimeType = resultSet[i].MimeType;    
  68.             var body = resultSet[i].DocumentBody;    
  69.             images[i] = "data:" + mimeType + ";base64," + body;    
  70.         }    
  71.         changeImage(0);    
  72.     
  73.     } else    
  74.         showImageGalery(TabName, SectionName, false);    
  75. }    
  76. //change image     
  77. function changeImage(counter) {    
  78.         
  79.     currentImage += counter;    
  80.     if (currentImage != TotalImages) {    
  81.         if (currentImage < 0)    
  82.             currentImage = 0;    
  83.     } else {    
  84.         currentImage = 0;    
  85.     }    
  86.       
  87.     img.src = images[currentImage];    
  88.     document.getElementById('imageGallery').appendChild(img);    
  89. }    
  90.     
  91. < /script>< div >< input onclick = "getnotesImages()"    
  92.    alt = "Reload"    
  93.    type = "image"    
  94.    src = "../Image/Refresh.png" >< input class = "left-div"    
  95.    alt = "Go Next"    
  96.    onclick = "changeImage(-1);"    
  97.    type = "image"    
  98.    src = "../Image/Back.png" >< input class = "right-div"    
  99.    alt = "Go Back"    
  100.    onclick = "changeImage(1);"    
  101.    type = "image"       
  102.    src = "../Image/Next.png" >< /div>< div id = "imageGallery"    
  103.    style = "width: 100%; overflow: hidden;" >< /div>  
  104. </body >< /html>  

You can download complete utility from GitHub

The following are the steps to use this Gallery:

  1. Download Solution from the following link.
  2. Import this solution to your CRM environment (We can place this web resource to any entity which has relationship with notes)
  3. Let’s add this to account form. Open account entity form editor, and add a new section to existing tab or add a new tab and section to form.
  4. Double click on tab and make note of tab name, follow similar steps for section and make note of section name, we need to pass these to web resource.
  5. Select your section and click on Web Resource by navigating to insert tab under ribbon bar.
  6. Select web resource properties like the following: 



  7. Click OK and save, publish your changes.
  8. Open any account record and attach image using notes link in social pane



  9. Refresh your record first time to get image gallery, and you should be able to see it like the following: