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.
// useTableMetadata fetches the column + relationship metadata for one
// Dataverse table. Concurrent calls dedupe onto a single in-flight
// promise per table, and resolved values are reused for the lifetime
// of the PowerPortalsProClient. `data` is undefined while loading or
// when the table doesn't exist.
import { useTableMetadata } from '@powerportalspro/react';
function MyComponent() {
const { data: tableMetadata, status } = useTableMetadata('account');
if (status !== 'success' || !tableMetadata) return null;
const { tableName, primaryIdColumn, primaryNameColumn } = tableMetadata;
// ...
}[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.
const { data: tableMetadata } = useTableMetadata('contact');
// Look up a specific column. The columns array carries one entry per
// Dataverse column; narrow on the discriminated `type` field to reach
// type-specific properties.
const column = tableMetadata?.columns.find((c) => c.columnName === 'firstname');
if (column) {
const isRequired = column.isRequired;
const isValidForUpdate = column.isValidForUpdate;
if (column.type === ColumnType.String) {
const maxLength = column.maxLength;
}
}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.
const { data: tableMetadata } = useTableMetadata('account');
// Find a one-to-many relationship
const oneToMany = tableMetadata?.oneToMany.find(
(x) => x.relationshipName === 'contact_customer_accounts',
);
// Find a many-to-many relationship
const manyToMany = tableMetadata?.manyToMany.find(
(x) => x.relationshipName === 'ppp_Account_ppp_Region_ppp_Region',
);
if (manyToMany) {
const relatedTable = tableMetadata?.tableName === manyToMany.table1
? manyToMany.table2
: manyToMany.table1;
}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
ITableMetadataCacheoverIPowerPortalsProService.RetrieveTableMetadataAsyncfor reading table metadata. The cache avoids redundant network calls and is used internally by all PowerPortalsPro components.
ITableMetadataCache Interface
Methods
Name | Parameters | Type | Description |
|---|---|---|---|
GetAsync | string 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. |
GetAsyncCancellationToken token
