Skip to content
← All tips

How to get the primary name column of any Dataverse table

As soon as you write generic code, like a plugin that runs on several tables or a tool, you need to know the primary name column of each table. For accounts it’s name, for contacts fullname, for cases title. Rather than hardcoding it, I ask the metadata.

In C# a RetrieveEntityRequest is enough:

public static string GetPrimaryNameAttribute(IOrganizationService service, string logicalName)
{
    var response = (RetrieveEntityResponse)service.Execute(new RetrieveEntityRequest
    {
        LogicalName = logicalName,
        EntityFilters = EntityFilters.Entity, // only the table, no columns: much faster
    });
 
    return response.EntityMetadata.PrimaryNameAttribute; // PrimaryIdAttribute for the id
}

With the Web API it’s a single request:

GET /api/data/v9.2/EntityDefinitions(LogicalName='account')?$select=PrimaryNameAttribute,PrimaryIdAttribute

Metadata hardly ever changes, so there’s no point fetching it inside a loop. If you call this often, keep the result in a static variable or a cache.

Share:LinkedInX

Did it help?

If you need help with this in your project, write to me.

More tips