Programmatically Reuse Dynamics Crm 4 Icons

Gayan Perera, 10 March 2010

The team that wrote the dynamics crm sdk help rocks! I wanted to display the same crm icons on our time tracking application for consistency, so I opened up the sdk help file, searched for 'icon', ignored all the sitemap/isv config entries since I know I want to get these icons programatically, about half way down the search results I see 'organizationui', sure enough that contains the 16x16 (gridicon), 32x32 (outlookshortcuticon) and 66x48 (largeentityicon) icons!

 To get all the entities, execute a retrieve multiple request.

RetrieveMultipleRequest request = new RetrieveMultipleRequest
{
    Query = new QueryExpression
    {
        EntityName = "organizationui",
        ColumnSet = new ColumnSet(new[] { "objecttypecode", "formxml", "gridicon" }),
    }
};
 
var response = sdk.Execute(request) as RetrieveMultipleResponse;

 

Now you have all the entities and icons, here's the tricky part, all the custom entities in crm store the icons inside gridicon, outlookshortcuticon and largeentityicon attributes, the built-in entity icons are stored inside the /_imgs/ folder with the format of /_imgs/ico_16_xxxx.gif (gridicon), with xxxx being the entity type code. The entity type code is not stored inside an attribute of organizationui, however you can get it by looking at the formxml attribute objecttypecode xml attribute.

 

response.BusinessEntityCollection.BusinessEntities.ToList()
    .Cast<organizationui>().ToList()
    .ForEach(a =>
    {
        try
        {
            // easy way to check if it's a custom entity
            if (!string.IsNullOrEmpty(a.gridicon))
            {
                byte[] gif = Convert.FromBase64String(a.gridicon);
            }
            else
            {
                // built-in entity
                if (!string.IsNullOrEmpty(a.formxml))
                {
                    int start = a.formxml.IndexOf("objecttypecode=\"") + 16;
                    int end = a.formxml.IndexOf("\"", start);
 
                    // found the entity type code
                    string code = a.formxml.Substring(start, end - start);
                    string url = string.Format("/_imgs/ico_16_{0}.gif", code);

Enjoy!