ITableMetadataCache

The ITableMetadataCache service provides cached access to Dataverse table metadata. It retrieves table metadata from Dataverse on first access and caches it for subsequent requests, avoiding repeated round trips to the server.

Usage

Inject ITableMetadataCache and call GetAsync with the table's logical name. The first call retrieves the metadata from Dataverse; subsequent calls return the cached value.

[Inject]
private ITableMetadataCache _tableMetadataCache { get; set; } = null!;

protected override async Task OnInitializedAsync()
{
    var tableMetadata = await _tableMetadataCache.GetAsync("account");

    if (tableMetadata != null)
    {
        var tableName = tableMetadata.TableName;
        var primaryKey = tableMetadata.PrimaryIdColumn;
        var primaryName = tableMetadata.PrimaryNameColumn;
    }
}

What's Included

The returned TableMetadata object includes:

  • Columns — A collection of column metadata including data types, validation rules, labels, and constraints.
  • PrimaryIdColumn / PrimaryNameColumn — The logical names of the table's primary key and primary name columns.
  • OneToMany / ManyToOne / ManyToMany — Relationship metadata for navigating between related tables.
  • PrimaryImageColumn — The logical name of the table's primary image column, if one exists.

Accessing Column Metadata

Use the Columns collection to look up metadata for a specific column. Cast to the appropriate metadata type (e.g. StringMetadata, LookupMetadata, ChoiceMetadata) for type-specific properties like max length, target tables, or option values.

var tableMetadata = await _tableMetadataCache.GetAsync("contact");

// Look up a specific column
if (tableMetadata.Columns.TryGetColumn("firstname", out var column))
{
    var isRequired = column.IsRequired;
    var isValidForUpdate = column.IsValidForUpdate;

    // Cast to specific type for type-specific properties
    if (column is StringMetadata stringMeta)
    {
        var maxLength = stringMeta.MaxLength;
    }
}

Accessing Relationship Metadata

Use the OneToMany, ManyToOne, and ManyToMany collections to look up relationship metadata by relationship name. This is used internally by SubGrid and ManyToManyLookupEdit to resolve related tables and build queries.

var tableMetadata = await _tableMetadataCache.GetAsync("account");

// Find a one-to-many relationship
var oneToMany = tableMetadata.OneToMany
    .FirstOrDefault(x => x.RelationshipName == "contact_customer_accounts");

// Find a many-to-many relationship
var manyToMany = tableMetadata.ManyToMany
    .FirstOrDefault(x => x.RelationshipName == "ppp_Account_ppp_Region_ppp_Region");

if (manyToMany != null)
{
    var relatedTable = tableMetadata.TableName == manyToMany.Table1
        ? manyToMany.Table2
        : manyToMany.Table1;
}

Tip

Prefer ITableMetadataCache over IPowerPortalsProService.RetrieveTableMetadataAsync for reading table metadata. The cache avoids redundant network calls and is used internally by all PowerPortalsPro components.

ITableMetadataCache Class

Methods

Name
Parameters
Type
Description
GetAsyncstring key
CancellationToken token
Task<TableMetadata>
Retrieves an item from the cache by key. If the item is not cached, it is fetched from the underlying source, cached, and returned.
Name: GetAsync
Parameters: string key
CancellationToken token
Type: Task<TableMetadata>
Description: Retrieves an item from the cache by key. If the item is not cached, it is fetched from the underlying source, cached, and returned.