Редагувати

Поділитися через


Work with image column definitions using code

Use image columns to store image data. Image columns are optimized for storing binary data. Dataverse doesn't save this data in the relational data store, which improves performance and reduces the capacity usage. Learn more about storage capacity

A custom or customizable table can have zero or more image columns. This article is about working with column definitions in code. To use data stored in these columns, see Use image column data.

Create image columns

The recommended way to create image columns is to use Power Apps and define your columns using the designer. More information: Image Columns.

You can also create image columns using the Dataverse SDK for .NET or using the Web API. The following examples show how:

Use the ImageAttributeMetadata Class with the CreateAttributeRequest Class to create an image column.

public static void CreateImageColumn(IOrganizationService service, string entityLogicalName, string imageColumnSchemaName) {

    ImageAttributeMetadata imageColumn = new()
    {
        SchemaName = imageColumnSchemaName,
        DisplayName = new Label("Sample Image Column", 1033),
        RequiredLevel = new AttributeRequiredLevelManagedProperty(
                AttributeRequiredLevel.None),
        Description = new Label("Sample Image Column for ImageOperation samples", 1033),
        MaxSizeInKB = 30 * 1024, // 30 MB
        CanStoreFullImage = true

    };

    CreateAttributeRequest createimageColumnRequest = new() {
        EntityName = entityLogicalName,
        Attribute = imageColumn                   
    };

    service.Execute(createimageColumnRequest);

}

More information:

Update image columns

In addition to the properties inherited from the AttributeMetadata class | AttributeMetadata EntityType, you can also update the following properties of the ImageAttributeMetadata class | ImageAttributeMetadata EntityType.

Property Label Description
MaxSizeInKB Maximum image size Set this value to the smallest useable data size appropriate for your particular application. The default setting is 10240, or 10 MB. The maximum value is 30720 KB (30 MB). This value can't be changed in Power Apps using the designer after you create the image column, but you can change it using the API.
CanStoreFullImage Can store full images When this value is false, only thumbnail-sized images are available. Full images are stored in file storage on the Azure blob to reduce data storage consumption.

You can query the Image Attribute Configuration (AttributeImageConfig) table to find which image columns support full-sized images. More information: Detect which image columns support full-sized images
IsPrimaryImage Primary image column Whether the column is used to represent a table row in applications.

If there's only one image column for a table, this value is set by default. When another image column already exists for a table, this value is ignored if set to true when creating a new image column. However, you can update the column after you create it to make the new column the primary image column.

IsPrimaryImage can't be set to false, an exception is thrown if you try. You must choose another image column and set that IsPrimaryImage value to true.

If you delete a column that is the current primary image column, another image column for the table is selected automatically to be the current primary image column.

You can query the Entity Image Configuration (EntityImageConfig) table to know which image columns are the primary images for any table. More information: Primary Images

Note

The MaxHeight and MaxWidth values are always 144 and cannot be changed. These define the size of the thumbnail-sized images that are created for every image column value.

More information:

Retrieve image column definitions

Use these queries to retrieve image column definitions.

The static GetImageColumns method uses the RetrieveMetadataChangesRequest class to define a query to return details about all image columns in Dataverse or limited to a specific table.

The condition evaluated is whether the AttributeMetadata.AttributeTypeName property value equals AttributeTypeDisplayName.ImageType

/// <summary> 
/// Returns the image columns
/// </summary> 
/// <param name="service">The IOrganizationService</param> 
/// <param name="tableLogicalName">Optional filter by table logical name</param> 
static void GetImageColumns(IOrganizationService service, string tableLogicalName = "account") 
{ 
    // The Query definition 
    EntityQueryExpression entityQuery = new EntityQueryExpression() 
    {                 
        Properties = new MetadataPropertiesExpression("SchemaName","Attributes"), 
        AttributeQuery = new AttributeQueryExpression() 
        { 
            Properties = new MetadataPropertiesExpression( 
                "SchemaName", 
                "CanStoreFullImage", 
                "IsPrimaryImage", 
                "MaxSizeInKB") 
        } 
    }; 

    // Enable optional filtering by table logical name 
    if (!string.IsNullOrEmpty(tableLogicalName)){ 

        entityQuery.Criteria.Conditions.Add( 
            new MetadataConditionExpression( 
                propertyName: "LogicalName", 
                conditionOperator: MetadataConditionOperator.Equals, 
                value: tableLogicalName)); 
    } 

    // Only Image columns 
    entityQuery.AttributeQuery.Criteria.Conditions.Add( 
        new MetadataConditionExpression( 
            propertyName: "AttributeTypeName", 
            conditionOperator: MetadataConditionOperator.Equals, 
            value: AttributeTypeDisplayName.ImageType)); 


    // The request 
    RetrieveMetadataChangesRequest request = new RetrieveMetadataChangesRequest() {  
            Query = entityQuery 
    }; 

    // Send the request 
    var response = (RetrieveMetadataChangesResponse)service.Execute(request); 

    //Display the results: 
    response.EntityMetadata.ToList().ForEach(e => { 
        if (e.Attributes.Count() > 0) 
        { 
            Console.WriteLine($"Table: {e.SchemaName}"); 

            e.Attributes.ToList().ForEach(a => { 

                // To access image column properties 
                var  imageColumn = (ImageAttributeMetadata)a; 

                Console.WriteLine($"\t{a.SchemaName}"); 
                Console.WriteLine($"\t\tCanStoreFullImage:{imageColumn.CanStoreFullImage}"); 
                Console.WriteLine($"\t\tIsPrimaryImage:{imageColumn.IsPrimaryImage }"); 
                Console.WriteLine($"\t\tMaxSizeInKB:{imageColumn.MaxSizeInKB}"); 
            }); 

            Console.WriteLine(); 
        }             
    }); 
} 

More information: Query schema definitions

Restrictions with Customer Managed Keys (CMK)

The same restrictions that apply to file columns also apply to image columns configured to store full-sized images. More information: Restrictions with self-managed key (BYOK)

See also

Use image column data
Work with file column definitions using code
Sample: Image Operations using Dataverse SDK for .NET
Sample: Image Operations using Dataverse Web API