【问题标题】:how to query entity from RegardingObjectID?如何从有关ObjectID 中查询实体?
【发布时间】:2017-06-19 20:16:47
【问题描述】:

我们可以像这样查询 web api 端点:

GET [Organization URI]/api/data/v8.2/accounts?$select=name&$top=3 HTTP/1.1
Accept: application/json
OData-MaxVersion: 4.0
OData-Version: 4.0

当响应包含 aboutobjectid 字段时,我们如何针对该记录发出类似的调用?

{
 "@odata.context": "[Organization URI]/api/data/v8.2/$metadata#accounts(name)",
 "value": [
  {
   "@odata.etag": "W/\"501097\"",
   "name": "Fourth Coffee (sample)",
   "accountid": "89390c24-9c72-e511-80d4-00155d2a68d1",
   "regardingobjectid":"dfdc331f-1cff-4bce-acd7-4815b2e87a30"
  },
 ]
}

是否有odata方式查询有关objectid,如:

GET [Organization URI]/api/data/v8.2/Entity?regardingobjectid eq 'dfdc331f-1cff-4bce-acd7-4815b2e87a30'

【问题讨论】:

    标签: c# dynamics-crm dynamics-crm-2016 dynamics-crm-webapi


    【解决方案1】:

    另一种方法是检索相关的对象 id 值

    var req = new XMLHttpRequest();
    req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.1/emails(B8345099-6074-E711-810F-00155D6FD705)?$select=_regardingobjectid_value", true);
    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() {
        if (this.readyState === 4) {
            req.onreadystatechange = null;
            if (this.status === 200) {
                var result = JSON.parse(this.response);
                var _regardingobjectid_value = result["_regardingobjectid_value"];
                var _regardingobjectid_value_formatted = result["_regardingobjectid_value@OData.Community.Display.V1.FormattedValue"];
                var _regardingobjectid_value_lookuplogicalname = result["_regardingobjectid_value@Microsoft.Dynamics.CRM.lookuplogicalname"];
            } else {
                Xrm.Utility.alertDialog(this.statusText);
            }
        }
    };
    req.send();
    

    *请注意标题以包含 odata 注释。

    应该返回这个:

    {
    "@odata.context": "http://xxxx/api/data/v8.1/$metadata#emails(_regardingobjectid_value)/$entity",
    "@odata.etag": "W/\"633069\"",
    "_regardingobjectid_value@Microsoft.Dynamics.CRM.associatednavigationproperty": "regardingobjectid_account_email",
    "_regardingobjectid_value@Microsoft.Dynamics.CRM.lookuplogicalname": "account",
    "_regardingobjectid_value@OData.Community.Display.V1.FormattedValue": "Coho Winery (sample)",
    "_regardingobjectid_value": "b3cc84f2-be0d-e711-8104-00155d6fd705",
    "activityid": "b8345099-6074-e711-810f-00155d6fd705"
    }
    

    _regardingobjectid_value@Microsoft.Dynamics.CRM.lookuplogicalname 给我们实体名称和 _regardingobjectid_value 为我们提供了查询相关记录所需的 id:

    /api/data/v8.1/accounts(b3cc84f2-be0d-e711-8104-00155d6fd705)
    

    【讨论】:

      【解决方案2】:

      如果我们想过滤相关记录,这应该可以工作。

      GET [Organization URI]/api/data/v8.2/accounts?$select=name&$filter=_regardingobjectid_value eq guid
      

      注意:没有单引号的guid

      要从相关实体(单条记录)查询列值,请使用expand。例如 - 获取客户记录中的主要联系方式:

      ?$select=name&$expand=primarycontactid($select=fullname,jobtitle,annualincome)
      

      我会推荐 CRM REST Builder 来构建查询。

      参考:https://community.dynamics.com/crm/b/mscrmcustomization/archive/2016/10/18/ms-crm-2016-web-api-operations-retrieve-single-or-multiple-records

      【讨论】:

      猜你喜欢
      • 2021-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多