Retrieving Entity Metadata using web api

↘锁芯ラ 提交于 2019-12-06 15:48:34

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

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

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 + "'";
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!