Sample: Set and retrieve entity images

This sample shows how to set and retrieve data for entity images. You can download the sample from here.

Note

This sample applies to both Dynamics 365 Customer Engagement (on-premises) and Dataverse.

How to run this sample

To obtain a local copy of all samples and build them, do the following:

  1. Download or clone the Samples repo so that you have a local copy.
  2. (Optional) Edit the cds/App.config file to define a connection string specifying the instance/organization you want to connect to.
  3. Open the sample solution in Visual Studio and press F5 to run the sample. After you specify a connection string in cds/App.config, any sample you run will use that connection information.

If you do not specify a connection string in cds/App.config file, a dialog will open each time you run the sample and you will need to enter information about which instance/org you want to connect to and which credentials you want to use. This dialog will cache previous connections so that you can choose a previously used connection.

What this sample does

This sample shows how to set and retrieve data for entity images.

How this sample works

In order to simulate the scenario described in What this sample does, the sample will do the following:

Setup

Checks the current version of the org.

Demonstrate

  1. Uses the CreateImageAttributeDemoEntity method to create a custom entity with the schema name sample_ImageAttributeDemo and a primary attribute with the schema name sample_Name.

  2. Create an image attribute with the schema name EntityImage. All image attributes use this name.

  3. Retrieve and update the main form for the sample_ImageAttributeDemo entity to set the showImage attribute to true so that the image is displayed in the form.

  4. Publish the sample_ImageAttributeDemo entity.

  5. Creates five new records for the sample_ImageAttributeDemo entity using five different sized images located in the Images folder as shown here.After each record is created you have the opportunity to view the record in the web browser application using the ShowEntityFormInBrowser method so that you can see how the source images are resized to fit the space available in the form.

  6. Retrieves the records with the entityimage attribute and saves the resized files. After you run the sample you can find the files saved in the \bin\Debug folder.

  7. Retrieves the records with the entityimage_url attribute and displays the relative URL values you can include in your application to access the images. This query should be more responsive because the amount of data transferred is smaller.

Clean up

Display an option to delete the records in Setup. The deletion is optional in case you want to examine the entities and data created by the sample. You can manually delete the records to achieve the same result.