Search code examples
javascriptdynamics-crmmetadatadynamics-crm-onlinedynamics-crm-2016

How to get the entity type of a lookup from MetaData via Web API in JS


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` +
    `)`

Is it possible to select the Targets property?

Different solution approaches

These approaches come with their own drawbacks in my use case.

  1. Requesting all 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.
  2. Requesting exact one data record of the selected entity where the particular lookup is not null and including the annotation header, so that the lookuplogical name is present. This has the drawback, that no such record is found in the system. Also feels wrong to request data records instead of MetaData.

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="*"`
"[email protected]": "contact",
  1. Requesting the metadata in C# and invoking the code from JS via a custom action.

Solution

  • It is possible to select 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