How can we help?

Nathan Hartmann

 

Using JavaScript within Microsoft Dynamic CRM can be a real pain in the you-know-what. When we began building the administrative interface for Intelligent Query, our most recent experiment from The CRM Lab @ Cobalt, we needed to retrieve a list of entities and their icons so the end user could choose which entities they wanted to apply our solution to. As you’ll see below, we had to get a little bit creative when it came to getting entity icons out of CRM for both on-premise and CRM Online deployments supporting CRM 2013, CRM 2015 and beyond.

Let’s jump right into why you’re reading this post – the code. I’ll explain the reasoning for the logic afterwards.

function GetEntityIconUrl(entityMetadata)
 { var url = "/_imgs/ico_16_customEntity.gif"; //default system icon if (entityMetadata.ObjectTypeCode != null) { //gets the entity image URL of a custom Entity if (entityMetadata.ObjectTypeCode >= 10000 && entityMetadata.IconSmallName != null) { url = Xrm.Page.context.getClientUrl() + "/WebResources/" + entityMetadata.IconSmallName.toString();
                     //default icon

                     if (!UrlExists(url))
                     {
                          url = "/_imgs/ico_16_customEntity.gif";
                     }
           }

           //get system entity icons

           else
           {
                url = "/_imgs/ico_16_" +
                entityMetadata.ObjectTypeCode.toString() + ".gif";

                //not all system entities use GIF format

                if (!UrlExists(url))
                {
                     url = "/_imgs/ico_16_" 
                     +entityMetadata.ObjectTypeCode.toString() +
                     ".png";

                     //default icon

                    if (!UrlExists(url))
                    {
                         url = "/_imgs/ico_16_customEntity.gif";
                    }
                }
           }
      }
      return url;
} function UrlExists(url) {      var http = new XMLHttpRequest();
     http.open('HEAD', url, false);
     http.send();
     return http.status != 404;
}

This snippet of JavaScript is expecting that you pass in the metadata (https://msdn.microsoft.com/en-us/library/jj919080.aspx) of the entity in which you are attempting to retrieve an icon for. The reason why we need the entity metadata as opposed to just the object type code of the entity is because the icon small name property is the most reliable way in which one can get custom entity icons out of the system across the various CRM environments. For system icons, only the majority of image files in CRM are formatted as GIFs. So a check is required to make sure a GIF format of the image exist and a PNG formatted version of the image is attempted to be used as a fall back. In all cases, the default entity icon is used if the image URL does not exist at all.

We used this to build a really cool interface for selecting CRM entities, but it’s applicable to any application where you’d like to show the user the entity icons.

Keep updated with our news & articles.