Saving Images in MS CRM
Have you ever found yourself wanting to store images as attributes on your entities? Out of the box Microsoft CRM does not directly support image attributes, leaving you to come with an alternation solution such as store the images on a file store and linking the path as a string attribute in the CRM database.
My first thought was to simply store images as a base64 string in a text field, but the largest text field supported (i.e. documented) is 5,000 characters – not quite enough for my favorite picture of my dog, Loki. However, since Microsoft CRM supports one-to-many relationships, it occurred to me the base64 string could be broken up into 5k chunks and stored as child entities to a master image entity.
The actual construction of the relationships is rather straightforward, but the real problem comes when associating an image to a specific entity. If you make the image a child of the entity, then it is not possible to associate other types of entities with the same image entity. What if you flip the relationship? After poking around in the database I found some of the built-in entities supported this type of relationship, in a loosely coupled manner. Rather than create a direct relationship between entities and images, you can create references to the entities Object ID with an associated Object Type Code to complete the relationship.
Once I put all of these pieces together I was able to add images to any entity in the system. Displaying them just takes reassembling the pieces and streaming them back to the browser. A topic for another day…