Dynamics CRM SSRS Image Solution

Date: 21/02/2016 | Dynamics CRM, SQL Reporting, SSRS

You might have come across a problem in Microsoft Dynamics CRM on-premise /cloud where you have an issue with  getting images to render after you've deployed your .rpt file. The problem  I experienced in particular seemed to be the result of an "unattended execution account" problem.  Fortunately; I did find a work around to the problem and if there are developers out there with a better solution please feel free to post or ping me . At the time of this writing, the only solution I've found was to develop a custom  plugin that will allow you to convert an image byte array into a base64 string on an update event. From here I could simply then use "custom code" in SSRS to convert the base64 string into an image again. The steps to resolve the problem were fairly easy I simply did the following :   Step 1 - Add a multiple line text type field to the selected entity. In my example; I added a field called "Base64Image" and set the maximum length to 100,000.  See below : imagefix-config IMPORTANT : You may want to put constraints around the size of the image being uploaded or at the very least expand your maximum length of the multi line text field to something greater Step 2 - Write  your plugin. I've written an late bound Dynamics CRM plugin in C# on my contact entity. Now this will be need to be triggered on an update (which I'll show later). Here is the code I wrote : using Microsoft.Xrm.Sdk;

using Microsoft.Xrm.Sdk.Query;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SSRSBase64Image
{
    public class Base64ImagePlugin : IPlugin
    {
        /// <summary>
        /// A plugin to convert uploaded images into a base 64 string 
        /// </summary>
        /// <remarks>Register this plug-in on the Create message, contact entity,
        /// and asynchronous mode.
        /// </remarks>
        public void Execute(IServiceProvider serviceProvider)
        {
            

            // Obtain the execution context from the service provider.
            IPluginExecutionContext context = (IPluginExecutionContext)
                serviceProvider.GetService(typeof(IPluginExecutionContext));

            // The InputParameters collection contains all the data passed in the message request.
            if (context.InputParameters.Contains("Target") &&
                context.InputParameters["Target"] is Entity && context.Depth == 1)
            {
                // Obtain the target entity from the input parameters.
                Entity entity = (Entity)context.InputParameters["Target"];

                if (entity.LogicalName != "contact")
                    return;

                try
                {
                    // Obtain the organization service reference.
                    IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                    IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

                    // Instaniate an contact object.
                    Entity contact = new Entity("contact");

                    // Create a column set to define which attributes should be retrieved.
                    ColumnSet attributes = new ColumnSet(true);

                    // Retrieve contact.
                    contact = service.Retrieve(contact.LogicalName, entity.Id, attributes);

                    if (!contact.Attributes.ContainsKey("new_base64image"))
                    {
                        contact.Attributes.Add("new_base64image", "");
                    }
                    
                                   
                   contact["new_base64image"] = Convert.ToBase64String((byte[])entity["entityimage"]);

                    service.Update(contact);
                }


                catch (Exception ex)
                {
                   throw ex;
                }
            }
        }
    }
}

Step 3- The next step is to compile and then register your plugin using the CRM SDK "Plugin Registration" tool. Once you have done this you will then next to register your "Update" step. Once again I'll be using contact as an example: See below : setup Step 4- Create your SSRS report with and create your image box. Now I won't go into how to create the SSRS report but I will tell you that you need to set your image box source type as external. Now to convert the image the image from a base64 string to an image with the following line of code : = System.Convert.FromBase64String(Fields!base64textvalue.Value). Yes... It really is that simple. Step 5- The final step, simply add / upload your report to your CRM solution and test. That's that. Enjoy and feel free to comment or send me a message if you get into any troubles. Thanks you for reading.


Posted by: Shaun Case
Position: Technical Consultant
Date: 21/02/2016

Share On: