Dynamics 365 introduced a lot of new features and improvements over the previous versions to offer more control and functionalities to the user. The ability to retrieve metadata using name is one such improvement in Web API.
Previously, to retrieve entity metadata, users needed to use ‘metadataid’. With the addition of this improvement, users can now retrieve the Entity/Attributes/Global Option Set meta by using the logical name.
When do we need to retrieve metadata?
There are many scenarios where the user needs object type code, primary attributes, localized label, list of entity attributes, etc. or when the user wants option set metadata. In such situations the user needs to read the entity metadata which can now be easily retrieved.
Let us see an example to understand how to retrieve Entity metadata:
- To retrieve Contact entity metadata using Name, use the script mentioned below;
//this function is used to retrieve entityMetadata function retrieveEntityMetaData() { var entityName = ""; try { //set variable value entityName = "contact"; //create AJAX request $.ajax({ type: "GET", contentType: "application/json; charset=utf-8", datatype: "json", url: encodeURI(this.getWebAPIPath() + "EntityDefinitions(LogicalName='" + entityName + "')"), beforeSend: function (xhr) { //Specifying this header ensures that the results will be returned as JSON. xhr.setRequestHeader("Accept", "application/json"); xhr.setRequestHeader("Accept", "application/json"); xhr.setRequestHeader("Content-Type", "application/json; charset=utf-8"); xhr.setRequestHeader("OData-MaxVersion", "4.0"); xhr.setRequestHeader("OData-Version", "4.0"); }, success: function (data, textStatus, xhr) { //Call successCallback retrieveEntityMetaDataSuccess(data); }, error: function (xhr, textStatus, errorThrown) { errorCallback(xhr); } }); } catch (e) { showMessage(e.message); } }
- The ‘success callback’ function mentioned below will get the metadata;
//success call back function retrieveEntityMetaDataSuccess(data) { try { if(data != null) { //get objectTypeCode var objectTypeCode = data.ObjectTypeCode; //get primary name var primaryAttribute = data.PrimaryNameAttribute; } } catch(e) { showMessage(e.message); } }
- If you want to retrieve metadata of any particular attribute then pass the following in url parameter in Ajax request;
url:encodeURI(this.getWebAPIPath() + "EntityDefinitions(LogicalName='" + entityName + "')/Attributes(LogicalName='parentcustomerid')") , success: function (data, textStatus, xhr) { //Call successCallback successCallback(data); },
In successCallback function, use data.value to get the attribute metadata.
- If you want to retrieve all the entity attributes then pass the following in url parameter in Ajax request;
url:encodeURI(this.getWebAPIPath() + "EntityDefinitions(LogicalName='" + entityName + "')/Attributes")
In successCallback function, use data.value to get the list of entity attributes.
- If you want to retrieve relationship metadata then you can use the following in url parameter in Ajax request;
url: encodeURI(this.getWebAPIPath() + "RelationshipDefinitions(SchemaName='account_primary_contact')")
In successCallback function, use data.value to get the relationship metadata.
- If you want to retrieve Global option set metadata then use the following in url parameter in Ajax request;
url: encodeURI(this.getWebAPIPath() + "GlobalOptionSetDefinitions(Name='budgetstatus')")
In successCallback function, use data.Options to get all options.
Conclusion:
Now with the addition of the ability to retrieve metadata using name in Dynamics 365, users can easily retrieve metadata.
One Pic = 1000 words! Analyze data 90% faster with visualization apps!
Get optimum visualization of Dynamics 365 CRM data with –
Kanban Board – Visualize Dynamics 365 CRM data in Kanban view by categorizing entity records in lanes and rows as per their status, priority, etc.
Map My Relationships – Map My Relationships – Visualize connections and relationships between Dynamics 365 CRM entities or related records in a Mind Map view.