问题
we have a requirement where we have to retrieve an Entity's Metadata. Exact requirement: Im reading a field value on a form which ia having "entity schema name". With that I need to get that Entity's primary key schema name. Is it possible? If so please help me. Eg: in that field if I enter "lead" , that web api should fetch me "leadid" and store it in another field. 2. If I enter "incident" , that web api should get me "incidentid"
回答1:
You don't need do retrieve entity metadata for that, primary key is always "entity schema name" + "id", there are no exceptions from that rule. If you still want to retrieve metadata, you should be able to do it by calling:
https://crmaddress/api/data/v8.2/EntityDefinitions(LogicalName='account')/Attributes?$select=LogicalName
That would return all attributes for entity "account", primary key is the one without any "@odata.type" attribute
回答2:
Yeah, I do agree that there is no need to retrieve if it's primary key schema name as for each entity it's entity schema name + id (Lead + id = leadid). But, we felt it's not a good practice. We achieved this with the following code... It's perfectly working fine. When we provide correct Entity Schema Name, it will automatically populate that Primary Id Attribute into another field. new_primarykey - where I am populating the Primary Key Schema Name on entering Entity Schema Name in new_entityschemaname fieldon the form.
function getPrimaryKey() {
var Oldprimary = Xrm.Page.data.entity.attributes.get("new_primarykey").getValue();
var req = new XMLHttpRequest();
var entityName = Xrm.Page.data.entity.attributes.get("new_entityschemaname").getValue();
var url = Xrm.Page.context.getClientUrl() + "/api/data/v8.2/" + "EntityDefinitions?$select=PrimaryIdAttribute&$filter=SchemaName eq '" + entityName + "'";
req.open("GET", url, false);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
req.onreadystatechange = function () {
Xrm.Page.data.entity.attributes.get("new_primarykey").setValue("");
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 200) {
var results = JSON.parse(this.response);
var primarykey = results.value[0].PrimaryIdAttribute;
Xrm.Page.data.entity.attributes.get("new_primarykey").setValue(primarykey);
}
else {
Xrm.Utility.alertDialog("Error");
}
}
}
req.send();
};
enter image description here
回答3:
First of all, thanks for Sharing. I am currently working on a global button on form level which JavaScript that should get the primary key of the specific entity. I also started with entityName + "id", but this will fail on activity entities like email etc. So I started to implement the above.
When you get the entity's logical name via a form in javascript:
var entityName = Xrm.Page.data.entity.getEntityName();
You get for example "opportunity" and when you add this as the var for entityName
to the getPrimaryKey
this will fail because the SchemaName
of the entity is Opportunity and not opportunity. Also for Account etc.
So my advice is when you work with .getEntityName()
to use LogicalName
instead of SchemaName
which results in the following URL:
var url = Xrm.Page.context.getClientUrl() + "/api/data/v8.2/" + "EntityDefinitions?$select=PrimaryIdAttribute&$filter=LogicalName eq '" + entityName + "'";
来源:https://stackoverflow.com/questions/44395442/retrieving-entity-metadata-using-web-api