In this blog I will cover how to retrieve Entity Metadata via the CRM SDK. There are a variety of reasons that you may want to do this in Microsoft Dynamics 365. Recently, I’ve needed to retrieve the display name for an entity while working with a generalised control. The request itself is an OrganizationRequest, so it is executed as normal by using the execute method on the OrganizationService.
The procedure for retrieving entity metadata is relatively straightforward. Let’s first take a look at the general structure of the request, with the required fields used.
In order to retrieve the entity metadata, you first need to perform a RetrieveEntityRequest, for which the response is basically just a container for the Entity Metadata.
The required parameters for the request are:
Either LogicalName or MetadataId is required to inform the request which Entity it is you wish to be returned.
From the response, the EntityMetadata can be accessed. What information is contained here is determined by the EntityFilters defined in the request. In our case, this is the Entity’s attributes.
Using the display name is a little roundabout. The code below shows how to use the retrieved metadata to access and use the display name of the entity.
It’s important to note that before you use the Display Name, you need to get the UserLocalizedLabel, and then the Label attribute of that, as the Display Name initially retrieved cannot be implicitly converted to a string, and the ToString() method for which will instead return the type Label object.