Sam's Microsoft Dynamics 365 Blog

Wednesday, 3 October 2018

Display Image of Product in Dynamics Portal


In this blog, i am going to discuss about “Image” type attribute/field display in Dynamics Portal. Here we have an Image for a Product, we will display this image on Dynamics Portal.





I am creating a Web Template with name Display_image_Product.




Put below code in Source.

// Get Product Id from URL and assign it in pid
 {% assign pid = request.params['id'] %}
   {% fetchxml my_query %}
    <fetch version="1.0" mapping="logical">
    <entity name="product">
      <attribute name="name" />
      <attribute name="productid" />
      <attribute name="entityimage" />
      <attribute name="entityimage_url" />
<filter type="and">
<condition attribute="productid" value="{{ pid }}" uitype="product" operator="eq"/>
</filter>
     </entity>
  </fetch>
{% endfetchxml %}

 {% for result in my_query.results.entities %}
<img data-entityimage="{{ result.entityimage | join: ',' }}" />

<script type="text/javascript">

function toBase64(str) {
if (!str) return null;
var uarr = new Uint8Array(str.split(',').map(function (x) { return parseInt(x); }));
return btoa(String.fromCharCode.apply(null, uarr));
}

// Find any entity images and convert the byte string to base64
window.addEventListener('load', function () {
document.querySelectorAll('img[data-entityimage]').forEach(function (img) {
var data = img.dataset && img.dataset.entityimage;
var base64data = data ? toBase64(data) : null;
if (base64data) {
img.src = 'data:image/jpeg;base64,' + base64data;
}
});
});
</script>
          
 {% endfor %}


In above code it will get a Product Guid and will fetch its related image.



Here i have used in my Portal and it looks like:-


11 comments:

  1. Did not work for me. Entity Image is empty

    ReplyDelete
  2. It worked after clearing cache (/services/about)

    ReplyDelete
  3. Words will not be enough for me to appreciate Doctor Abolo of Abolospell@gmail.com who lives in Africa, for the successful spells he has been casting for me, I have never trusted any anybody, the way I trust him. This great dr was the one that saved my marriage. If you use him, I promise, you will trust him too. Any spell he cast for me is 100%. I see results the very same day. He also cured me from a deadly virus (HSV), a virus I have known as a medical, it doesn’t have a cure, he cured a patient I introduced to him to get pregnant after 15 years of marriage. He has active herbal remedies for diseases, infections and viruses. you can get him via; Email: Abolospell@gmail.com or you may decide to send him a msg on whatsapp here +27743411428. thanks doctor you are the best.

    ReplyDelete
  4. is anyone getting the below when tryin to access the entity image?
    img data-entityimage="Liquid error: Object of type 'Adxstudio.Xrm.Web.Mvc.Liquid.ImageDrop' cannot be converted to type 'System.Collections.IEnumerable'.

    ReplyDelete
    Replies
    1. this no longer works because of an update by msft, it has become easier, you can get the url of the image for the portal out of the image object.
      Just set the source attribute of your img element to: src="{{ result.entityimage.url }}"

      Delete
  5. Yes it just started 2 days ago.

    ReplyDelete
  6. Hi Dear,

    I really prefer your blog..! This blog are very useful for me and other. So I see it every day.
    Check out For Microsoft Dynamics 365 at Xrelements.com Microsoft Dynamics 365 customer service Australia XR Elements provides expert Microsoft Dynamics 365 solutions for customer service in Australia. Elevate your customer service capabilities, optimize workflows, and drive customer satisfaction with our tailored customer service-focused Dynamics 365 solutions.

    Visit Now - https://www.xrelements.com.au/dynamics-365/customer-service/

    Thanks.

    ReplyDelete
  7. I must be doing something wrong, I've created the web template and added the relevant code, but nothing is shown next to the account name ( I am trying to get images to show next to logged in user's account). Do I need to add these images to portal management first?

    ReplyDelete

Web Resource vs PCF vs Canvas App - which of the one is used?

While started working on specific Business Requirements related to custom layout, there is a always common question that "where to star...