This API has been introduced for sometime now. However I still find that consultants are not using it and going with traditional querying through WebAPI method to get the Entity Metadata.
And there is a secret I am going to tell you why so many consultants abandoned this method. The reason for this is when this method was introduced it had a significant bug. So as per the documentation the nomenclature of the function goes like this.
Xrm.Utility.getEntityMetadata(entityName,attributes).then(successCallback, errorCallback)
To get the metadata for attributes, you would need to pass array of strings. Even if you did that, it shall return only statecode and statuscode field. It was certainly a bummer. But I just used it for a project and it seems to work just fine.
If you just want to get the Entity Metadata and not the attributes, pass empty array
Xrm.Utility.getEntityMetadata(“account”, []).then(
function(s){
// handle you success code here
}, function(e){
console.log(e.error.message);
});
The below code looks for account entity metadata with couple of attributes for account entity. Name is of type string and IndustryCode is of type optionset. In the sample code I have shown how to go through Options metadata as well.
Xrm.Utility.getEntityMetadata(“account”, [“name”, “industrycode”]).then(
function (s) {
var attributeCollection = s.Attributes().getAll();
for (var a = 0; a < attributeCollection.length; a++) {
var attrMetadata = attributeCollection[a];
switch (attrMetadata.Type) {
case “picklist”: {
// get the options
var optionsArr = attrMetadata.OptionSet;
for (var k = 0; k < optionsArr.length; k++) {
var option = optionsArr[k];
console.log(“Text”, option.text);
console.log(“Value”, option.value);
}
break;
}
}
}
}, function (e) {
console.log(e.error.message);
});
Debajit Dutta
(Dynamics MVP & MCT)
For consultation/ corporate training visit www.xrmforyou.com or reach out to us at info@xrmforyou.com
Our product offerings:
Role based views for Dynamics 365 (http://www.xrmforyou.com/role-based-views.html)
CRM-Sharepoint Attachment uploader and metadata manager (http://www.xrmforyou.com/sharepoint-integrator.html)
Record Cloner for Dynamics 365 (http://www.xrmforyou.com/record-cloner.html)
Multiselect picklist for Dynamics 365 (http://www.xrmforyou.com/multi-select-picklist.html)
Discover more from Debajit's Power Apps & Dynamics 365 Blog
Subscribe to get the latest posts sent to your email.