Query and display entity image in your entity list or entity form in PowerApps/ Dynamics 365 Portals–Part 1

For training, consulting and our products, write to us at info@xrmforyou.com

This is a very common requirement. Showing entity image in your Dynamics 365 portals. And here I am going to show you not one but couple of ways to do the same. The first one is quite unique and less resource demanding on the portal page and relies more on your CRM configuration. My next post here completely takes the control to portal for to show the entity.

So let’s see the first method. For both I am showing for entity list. But the same concept can be applied for Entity forms.

In this method, I create a new field for the entity in Dynamics 365 for which I need to show entity image. I have used contact entity here.

I go ahead and create a new field in the contact entity. Below are the field details

image

The length of the field does not matter. I shall explain that in a moment.

The I write a plugin and register it in the POST Retrieve Multiple of contact entity. Below is the plugin code.

public class Class2 : IPlugin
    {
       public void Execute(IServiceProvider serviceProvider)
       {
          var context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

 

         var outputCollection = (EntityCollection)context.OutputParameters[“BusinessEntityCollection”];

 

         context.OutputParameters[“BusinessEntityCollection”] = this.ModifyOutput(outputCollection);
       }

 

      public EntityCollection ModifyOutput(EntityCollection output)
       {
          foreach (var entity in output.Entities)
          {
             if (entity.Attributes.Contains(“entityimage”))
             {
                var entityImage = entity.GetAttributeValue<byte[]>(“entityimage”);
                entity[“cr6b0_imagecontentbase64”] = Convert.ToBase64String(entityImage);
             }
          }

 

         return output;
       }
    }

Very simple isn’t it? All it is doing is checking if the entityimage field is there in output collection and then setting the description field with the base64 content of the image. Observe there is no database call here. All I am doing is setting the base64 content value in the field.

And that’s why the length of the description field does not matter, no matter how much is the size of the image. Because we are never storing it in the database. It’s being set at runtime and then used directly in output.

All set and done.

The next step is to create a web template. Below is my web template code

image

That’s all I need to do. And then you will see the entity image being displayed on your portal.

Hope you liked this. In my next post I am going to explain how we can achieve the same using different way without much CRM involvement.

Hope this helps!

Debajit Dutta

(Business Solutions MVP)

For training and consulting, write to us at info@xrmforyou.com


Discover more from Debajit's Power Apps & Dynamics 365 Blog

Subscribe to get the latest posts sent to your email.

Discover more from Debajit's Power Apps & Dynamics 365 Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading