Dowload Dynamics 365 Entity Record Photo in C#

Leave a comment

In Dynamics 365, records can have photos attached to them. For example, let’s say we have a custom entity called Guests, which have the entity photo attached:

In this post, we will create a C# app to download all the images on this entity using C#.

First, create a new console app in Visual Studio:

Install Xrm Tooling:

There are a few fields related to the entity image. These are:

  • entityimage – The entity image itself
  • entityimageid – The entity image guid
  • entityimage_timestamp – The time the image was uploaded
  • entityimage_url – The full URL to the image in Dynamics 365
  • entityimageid – The guid associated with the image

In our example, our custom entity is called new_guest. Add the code:

using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Tooling.Connector;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Carl.DownloadD365Photos
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                var connectionString = @"AuthType=Office365; Url=https://yourinstance.crm.dynamics.com/;Username=admin@yourinstance.onmicrosoft.com;Password=yourpassword";
                CrmServiceClient conn = new CrmServiceClient(connectionString);

                IOrganizationService service;
                service = (IOrganizationService)conn.OrganizationWebProxyClient != null ? (IOrganizationService)conn.OrganizationWebProxyClient : (IOrganizationService)conn.OrganizationServiceProxy;

                string query =
                String.Format(@"<fetch mapping='logical'>
                    <entity name='{0}'>
                    <attribute name='new_name' />
                    <attribute name='entityimage' />
                    <attribute name='entityimage_timestamp' />
                    <attribute name='entityimage_url' />
                    <attribute name='entityimageid' />
                    </entity>
                </fetch>", "new_guest");

                EntityCollection images = service.RetrieveMultiple(new FetchExpression(query));
                foreach (Entity record in images.Entities)
                {
                    string photoName = record["new_name"] + ".jpg" as string;
                    string entityimage_url = record["entityimage_url"] as string;
                    String file = String.Format("{0}", photoName);
                    byte[] image = record["entityimage"] as byte[];
                    var fs = new BinaryWriter(new FileStream(file, FileMode.Append, FileAccess.Write));
                    fs.Write(image);
                    fs.Close();
                    Console.WriteLine(file);
                }

                Console.ReadLine();
            }
            catch (Exception ex)
            {
                // Handle exception
            }
        }
    }
}

Run the code. You will see the files downloaded to your directory:

 

ABOUT CARL DE SOUZA

Carl de Souza is a developer and architect focusing on Microsoft Dynamics 365, BI, Web, Cloud and Data Science.

carldesouza.comLinkedIn Twitter | YouTube

 

Leave a Reply

Your email address will not be published. Required fields are marked *