How to render CRM image into SSRS Report?

banner

How to render CRM image into SSRS Report?

Introduction :

CRM only allow entity image field in order to save image in CRM Record.

You cannot create any image field for any Entity.

So if you want to add multiple image fields in Entity Form then it is not possible.

But this is not only the road block, if you have any requirement in which you need to create report for this entity in which you need to set the image as well then?

How to render CRM

The data type of entity image field is “Image” and in SSRS report you can’t use direct image as datatype and populate that image in report.

If you want to populate image in SSRS report dynamically then you have to covert that image in “Base64” type first and then you can use it in your SSRS report.

Description & Solution :

You have image for each entity record but the challenge it how to convert it into Base64 format and render it on SSRS report so when user generate report he/she will find the image set for the record.

To do so you have to do few customizations in to your CRM organisation as stated below.

  1. Create one multi line field in your CRM Entity and set its maximum length as 100,000. Also make sure the image size which you are uploading should not more than 5 megabytes otherwise there may a chance that this solution gets fail.
  2. Write one late bound plugin which will convert image into Base64 type and set the value into newly created multi line field. Also make sure you register this plugin on create of entity record and on update of All Attribute.
  3. Please find the sample plugin code below which I write for Contact entity.
using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Sdk.Query; using System; using System.Collections.Generic; using System.Linq; using System.ServiceModel; 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) { ITracingService tracingService = (ITracingService)ServiceProvider.GetService(typeof(ITracingService)); //Create the context IPluginExecutionContext context = (IPluginExecutionContext)ServiceProvider.GetService(typeof(IPluginExecutionContext)); IOrganizationServiceFactory ServiceFactory = (IOrganizationServiceFactory)ServiceProvider.GetService(typeof(IOrganizationServiceFactory)); IOrganizationService service = ServiceFactory.CreateOrganizationService(context.UserId); try { switch (context.MessageName.ToUpper()) { case "CREATE": Entity target = (Entity)context.InputParameters["Target"]; ConvertImageintoBase(service, tracingService, context); break; case "UPDATE": target = (Entity)context.InputParameters["Target"]; ConvertImageintoBase(service, tracingService, context); break; } } catch (FaultException ex) { throw new Exception(System.Reflection.MethodBase.GetCurrentMethod().Name + " :: " + ex.Message); } catch (Exception ex) { throw new Exception(System.Reflection.MethodBase.GetCurrentMethod().Name + " :: " + ex.Message); } } private void ConvertImageintoBase(IOrganizationService service, ITracingService tracingService, IPluginExecutionContext context) { try { 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; // 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); // new_base64entityimage is newly created multiline field. if (!contact.Attributes.ContainsKey("new_base64entityimage")) { contact.Attributes.Add("new_base64entityimage", ""); } Entity binaryData = service.Retrieve("contact", entity.Id, new ColumnSet("entityimage")); if (binaryData.Contains("entityimage")) { contact["new_base64entityimage"] = Convert.ToBase64String((byte[])binaryData["entityimage"]); } service.Update(contact); } catch (FaultException<OrganizationServiceFault> ex) { throw new Exception(System.Reflection.MethodBase.GetCurrentMethod().Name + " :: " + ex.Message); } catch (Exception ex) { throw new Exception(System.Reflection.MethodBase.GetCurrentMethod().Name + " :: " + ex.Message); } } } }
  1. Next step is build the plug and register it into your CRM Organization with the help of Plug-in Registration utility tool.
  2. Now create your SSRS report with image box.
  3. Set your image box source type as external. Now to convert the image from a base64 string to an image, write below expression for image in SSRS report: =System.Convert.FromBase64String(Fields!base64textvalue.Value)
  4. The final step, simply add / upload your report to your CRM solution and test.

    That’s it..!!

This blog is very useful and easy to understand in order to manage and place your entity image into SSRS report for the same entity in Microsoft dynamics CRM customization. Create your report with less efforts using this blog and show your knowledge.