In my application I have a certain entity type selected and retrieve all field attributes of this entity from the EntityDefinitions
web API endpoint by expanding Attributes
($expand=Attributes
). For a certain attribute of type 'Lookup'
(AttributeType
) I want to know the type of the related entity. Because the attribute request contains so much data for every attribute, I am filtering the expand by selecting a few properties. Unfortunately it is seems to be not possible to select the Targets
property. Therefore, I have to either request all the data (which is slow) or I am filtering and don't know the entity type.
Here is my request which works when Targets
is omitted.
`EntityDefinitions?` +
`$filter=EntitySetName eq '${entityTypeName}'&` +
`$select=Attributes&` +
`$expand=Attributes(` +
` $select=DisplayName,LogicalName,Targets` +
`)`
Targets
property?Different solution approaches
These approaches come with their own drawbacks in my use case.
RelationshipDefinitions
data at once. It contains all the necessary for every entity. The received data is huge and the request take quite a few seconds. Therefore the response is should be cached in the localStorage of the users browser. Filtering the request by only necessary properties seems to be not possible simarly to the EntityDefinitions.Example: account entity, get entity type of primarycontactid lookup field attribute. Url, header and response:
http://SYSTEM_URL/ORGA_NAME/api/data/v8.2/accounts?$filter=primarycontactid ne null&$top=1
'Prefer': `odata.include-annotations="*"`
"_primarycontactid_value@Microsoft.Dynamics.CRM.lookuplogicalname": "contact",
Targets
While jotting down some notes for my question, I figured out that it is necessary to cast the Attribute to a certain derived type. In my case I cast it as a lookup type with LookupAttributeMetadata. Filtering by Targets
is possible on the cast type, because the derived type contains the Targets
property. The base type AttributeMetadata does not.
The response will only contain Lookup Attibutes including solely the properties LogicalName
, DisplayName
and Targets
.
http://SYSTEM_URL/ORGA_NAME/api/data/v8.2/EntityDefinitions(LogicalName='account')/Attributes/Microsoft.Dynamics.CRM.LookupAttributeMetadata?$select=Targets,LogicalName,DisplayName
This is officially documented and explained here: query-metadata-web-api