Guest Post - Entity Images in Dynamics CRM 2013 Explored

In this post, our Dynamics CRM MVP, Roohi Shaikh introduces the image attribute in Dynamics CRM 2013.

With Dynamics CRM 2013, you can now associate an image with a record. So you always wanted to add faces to contacts or pictures to products, well now you can do it OOB without any need for Customizations.

Yes with the introduction of image attribute you can now add fields to store images. Currently however you are restricted to being able to attach only one image per entity. On the entity design you can now specify the attribute to be used to display images for the records.

image

image

Once the entity is setup to accept images, the record form would show an image placeholder in the header area using which you can add/remove images from the record

image

You can add/update the image using the following code

//update the record with a new image

string path = @"C:\test.png";

byte[] uploaddata = null;

//download the image

using (System.IO.FileStream fileStream = new FileStream(path, FileMode.Open))

                    {

int size = (int) fileStream.Length ;

                        uploaddata = new byte[size];

                        fileStream.Read(uploaddata, 0, size);

                    }

Entity contact = context.CreateQuery("contact").Where(c => c.GetAttributeValue<Guid>("contactid").Equals(contactid)).First<Entity>();

                     //set the image data

                    contact.Attributes["entityimage"] =  uploaddata;

                    context.UpdateObject(contact);

                    context.SaveChanges();

Now that images have been added, you might want to have them included in the reports too. The current preview option that displays a report of a record does not include the image. Let us check how we could display images on custom reports.

The image field is available for reading through queries executed using the RetrieveMultiple API call. The following code will let you read the image file in the image format.

//Retrieve and download the binary images

string binaryImageQuery =

                @"<fetch mapping='logical'>

                <entity name='lead'>

                <attribute name='fullname' />

                <attribute name='entityimage' />

                </entity>

                </fetch>";

EntityCollection binaryImageResults = _serviceProxy.RetrieveMultiple(new FetchExpression(binaryImageQuery));

foreach (Entity record in binaryImageResults.Entities)

                {

String recordName = record["fullname"] as String;

String downloadedFileName = String.Format("Downloaded_{0}", recordName);

byte[] imageBytes = record["entityimage"] as byte[];

var fs = new BinaryWriter(new FileStream(downloadedFileName, FileMode.Append, FileAccess.Write));

                    fs.Write(imageBytes);

                    fs.Close();                   

                }

But if this same code is executed using the ExecuteFetch API call, the results are returned in xml format and therefore the image attribute returns only a string “System.Byte[]” not the actual image binary data in Base64 string.

ExecuteFetchRequest fetch = new ExecuteFetchRequest();

fetch.FetchXml = binaryImageQuery;

_service.Execute(fetch);

As a result of this, reports designed using FetchXML (reports in CRM Online) would not be able to include images on the report.

The sql queries executed from FilteredViews would however provide you access to the binary data of the image and you can attach this field to an image control in SSRS to display the image.

Use the following query to generate the DataSet

select con.FullName, con.Parentcustomeridname,con.entityimage from FilteredContact con

Add the image control to the report and bind it to the entityimage field

image

The result would be

image

Conclusion:

1. Images can be read through queries executed through Retrieve Multiple calls

2. You need to explicitly specify the image attributes to be included for it to be returned through API calls.

3. Images for now can only be added to reports through SQL queries for On-Premise installs.

About Guest Blogging

South Asia MVP Award Program introduces Guest Posts by the MVPs from the region. These posts would help readers to be in touch with the recent trends in technology and be up-to-date with knowledge on Microsoft products.

Author

image