Programmatically reuse Dynamics CRM 4 icons

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!

7 Comments

  • This is fantastic code! Exactly what I was looking for!

    Only thing is, what is that written in? Is it shorthand or something? I'm working in C# and can translate this into something that will work for me, but I want to understand what you posted!

    Thanks

  • It's using C# and .NET 3.5 along with LINQ

  • Thanks! I was able to dig around a little and make this work. Interesting... I'm going to have to look into this more!

  • Oh, and one last comment (for now)...

    Not all entities are returned through this method. Among them Activity (activitypointer), Activity Party(activityparty), Business Task(businesstask), etc.

    Besides the obvious of including the appropriate entity codes in the code, is there another way to derive those codes that are not in OrganizationUI?

    Thanks

  • Yup, you can use the metadata service to get all entities in crm (including the activitypointer)

  • Thanks for this post, it really helped me out a lot!

    One thing, how can I get the outlookshortcuticon or the largeentityicon?

  • The largeentityicon is inside the organizationui entity so if you include that in the above retrieve query you'll be able to get access it

Comments have been disabled for this content.