User Review
( votes)Introduction
Currently, we need to create http requests to retrieve the metadata of the entity or attributes of any entity. But now, Dynamics 365 introduced a Web API method i.e Xrm.Utility.getEntityMetadata() which helps you retrieve any entity metadata easily without creating any http requests. Please find below two examples to understand this method in detail:
1. Retrieve whole entity metadata
Let’s take one example, we want to check if the current entity is activity entity or not. For this query, you can simply use Xrm.Utility.getEntityMetadata, you need to just pass the entity logical name of the entity which you want to retrieve the metadata for, as shown below:
Xrm.Utility.getEntityMetadata(“account”, []).then(
function(entityMetadata){
//check for entity metadata
If(entityMetadata!=undefined){
//declare one flag which indicate current entity is activity entity
var isActivityEntity = false;
//check for IsActivity attribute
if (entityMetadata.IsActivity != undefined) {
//check current entity is activity entity using IsActivity attribute
if (entityMetadata.IsActivity == true) {
//set flag to true as current entity is Activity entity
isActivityEntity = true;
} else {
//set flag to false as current entity is not Activity entity
isActivityEntity = true;
}
}
}
}, function(e){
//error
alert(e.error.message);
});
Note: We pass the second parameter is empty [] to getEntityMetadata method because in the above example we need to retrieve all metadata of the Account entity.
After running the below code, we will get all the metadata of that entity such as logical name, entity set name, as shown in the below screenshot.
2. Retrieve metadata of selected attribute of any entity
Now, we want to retrieve any attribute metadata of any entity that you want in Dynamics 365. Let’s take one example; we want to retrieve metadata of the industry field of Account entity using the getEntityMetadata method. In this scenario, you need to pass those entity names and attributes list for which you want to retrieve metadata, as shown below.
Xrm.Utility.getEntityMetadata(“account”, [“industrycode”]).then(
function (entityMetadata) {
//check for attribute entity metadata
if (entityMetadata != undefined && entityMetadata.Attributes != undefined && entityMetadata.Attributes._collection != undefined) {
//check for industry type option set
if (entityMetadata.Attributes._collection[“industrycode”]) {
//get option set data
var industryCodeOptionSetData = entityMetadata.Attributes._collection[“industrycode”].OptionSet;
}
}
}, function(e){
//error call back
alert(e.error.message);
});
After running the above code you will get all options data in the industry field Option Set, as shown in the below screenshot:
Conclusion
With help of Xrm.Utility.getEntityMetadata() method you can retrieve metadata of any entity or metadata of any attributes of that entity, you no longer need to create an http request to achieve this.