【问题标题】:Retrieving Entity Metadata using web api使用 web api 检索实体元数据
【发布时间】:2017-06-06 16:37:28
【问题描述】:

我们有一个要求,我们必须检索实体的元数据。 确切要求:我正在读取具有“实体模式名称”的表单上的字段值。有了这个,我需要获取该实体的主键模式名称。是否可以?如果是这样,请帮助我。 例如:在该字段中,如果我输入 "lead" ,该 web api 应该获取我 "leadid" 并将其存储在另一个字段中。 2. 如果我输入“incident”,那个 web api 应该会得到我的“incidentid”

【问题讨论】:

    标签: javascript dynamics-crm dynamics-crm-webapi


    【解决方案1】:

    您不需要为此检索实体元数据,对于活动以外的实体,主键始终是“实体架构名称”+“id”。如果您想要一个通用的解决方案,您应该能够从元数据中获取此信息:

    https://crmaddress/api/data/v9.1/EntityDefinitions(LogicalName='account')
    

    并简单地获取结果的“PrimaryIdAttribute”,因此示例代码为:

    fetch("/api/data/v9.1/EntityDefinitions(LogicalName='account')")
       .then(response => response.json())
       .then(data => console.log(data.PrimaryIdAttribute));
    

    【讨论】:

    • 该规则有一些主要的例外情况。所有活动均不遵循该约定。任务的id列是activityid,不是taskid。
    • @Polshgiant 感谢您指出这一点。我更新了答案并添加了一些示例代码。
    【解决方案2】:

    是的,我同意如果它是主键架构名称,则无需检索,因为对于每个实体,它的实体架构名称 + id (Lead + id = Leadid)。但是,我们觉得这不是一个好习惯。我们通过以下代码实现了这一点......它运行良好。当我们提供正确的实体模式名称时,它会自动将该 Primary Id 属性填充到另一个字段中。 new_primarykey - 我在表单上的 new_entityschemaname 字段中输入实体架构名称时填充主键架构名称。

    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】:

      首先感谢分享。我目前正在处理表单级别的全局按钮,该按钮应该获取特定实体的主键。我也是从 entityName + "id" 开始的,但是这在电子邮件等活动实体上会失败。所以我开始实现上述内容。

      当您通过 javascript 中的表单获取实体的逻辑名称时:

      var entityName = Xrm.Page.data.entity.getEntityName();
      

      例如,您会得到“机会”,当您将其作为 entityName 的 var 添加到 getPrimaryKey 时,这将失败,因为实体的 SchemaName 是机会而不是机会。也适用于帐户等。

      所以我的建议是当您使用.getEntityName() 时使用LogicalName 而不是SchemaName,这会产生以下网址:

      var url = Xrm.Page.context.getClientUrl() + "/api/data/v8.2/" + "EntityDefinitions?$select=PrimaryIdAttribute&$filter=LogicalName eq '" + entityName + "'";
      

      【讨论】:

      • 欢迎来到 Stack Overflow。 Stack Overflow 不是一个讨论论坛,它是一个问答网站,您可以在其中提出可以回答而不是讨论的特定编程问题。我们在这里不是分享,我们是询问回答。答案问题字段应仅用于问题的完整答案。任何不是答案的内容都应该是评论(一旦您获得足够的声誉,您就可以这样做)或完全是一个新问题。
      猜你喜欢
      • 1970-01-01
      • 2022-07-14
      • 2012-08-10
      • 1970-01-01
      • 1970-01-01
      • 2016-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多