【问题标题】:Not able to fetch "Name" of lookup fields of Link-Entity (Web API + FetchXml)无法获取链接实体(Web API + FetchXml)的查找字段的“名称”
【发布时间】:2016-12-07 22:22:17
【问题描述】:

我有一些通过 Dynamics CRM Web API 执行的 fetchXml。 fetchXml 查询的构造如下:

<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">
  <entity name="new_someEntityA">
    <attribute name="new_lookupForSomeEntityA" />
  <link-entity alias="new_someEntityB" name="new_someEntityB" from="entityBId" to="entityAId" visible="false" link-type="outer">
    <attribute name="new_lookupForSomeEntityB" />
  </link-entity>
  </entity>
</fetch>

当我通过 Web API 发送此查询时,我会收到响应,并且“new_lookupForSomeEntityA”的值包括值 (GUID) 和格式化值(它的名称)。但是“new_lookupForSomeEntityB”的响应只包括 GUID,我找不到获取它的 GUID 和值的方法。我添加了一个标题记录:

"Prefer": "odata.include-annotations=OData.Community.Display.V1.FormattedValue"

但这似乎只是让我获得了主要实体的格式化值,而不是链接实体。这是 Web API 的限制还是我做错了什么?任何帮助将不胜感激。

【问题讨论】:

  • 您是否尝试将名称属性添加到&lt;link-entity&gt; 节点?
  • 是的,我试过了。在 SQL 中,该属性是“new_lookupForSomeEntityBName”,如果我添加它,我会得到一个返回,说明该属性不存在。如果我使用“名称”(小写),查询返回,但仍然只有一个 GUID,没有查找名称。
  • 您需要在实体 new_someEntityB 上指定名称属性的名称。您可以在实体主选项卡上的 CRM 解决方案中查找它。在 SQL 中,new_lookupForSomeEntityBName 是过滤视图的列,映射到实际表的主字段名称。
  • 我将其更改为:&lt;link-entity alias="new_someEntityB" name="new_someEntityB" name-attribute="new_name" from="entityBId" to="entityAId" visible="false" link-type="outer"&gt; &lt;attribute name-attribute="new_name" name="new_lookupForSomeEntityB" /&gt; &lt;/link-entity&gt; 当我这样做时,我得到与返回值相同的 id,只是:new_someEntityB_x002e_new_lookupForSomeEntityBid: [GUID]
  • 不要使用name-attribute属性,而只能使用&lt;attribute name="new_name"/&gt;

标签: dynamics-crm dynamics-crm-online dynamics-crm-2016 fetchxml dynamics-crm-webapi


【解决方案1】:

以下代码使用 Web API + FetchXml 返回链接实体选项集的格式化值。
针对 API 8.2 版进行了测试:

var oDataUrl = 'https://[your_org].crm4.dynamics.com/api/data/v8.2/';
var encodedFetchXml = encodeURI(`
<fetch top="10" no-lock="true" >
  <entity name="contact" >
    <attribute name="fullname" alias="contactName" />
    <link-entity name="incident" from="customerid" to="contactid" link-type="inner" alias="incident" >
      <attribute name="caseorigincode" alias="incidentOrigin" />
    </link-entity>
  </entity>
</fetch>
`);
$.ajax({
    type: "GET",
    contentType: "application/json; charset=utf-8",
    datatype: "json",
    url: `${oDataUrl}contacts?fetchXml=${encodedFetchXml}`,
    beforeSend: function (XMLHttpRequest) {
        XMLHttpRequest.setRequestHeader("Accept", "application/json");
        XMLHttpRequest.setRequestHeader("Prefer", "odata.include-annotations=OData.Community.Display.V1.FormattedValue");
    }
}).then(function (response) {
    // formatted value included in the response objects
    // incidentOrigin@OData.Community.Display.V1.FormattedValue:"WhatsApp"
    console.dir(response); 
}); 

这是返回的数据:
{ "@odata.context":"https://[your_org].crm4.dynamics.com/api/data/v8.2/$metadata#contacts(contactid)", "value":[{ "@odata.etag": "W/\"873006\"", "contactid": "ecfd1feb-d826-468a-bfe3-6ebd781c39f4", "contactName": "Ada Lovelace", "incidentOrigin@OData.Community.Display.V1.FormattedValue": "WhatsApp", "incidentOrigin": 269420000 }] }

它适用于内部和外部链接类型。

Link-Entity 属性别名与属性命名相同
如果您使链接实体属性的别名与属性名称相同,则根本不会返回该属性。此问题似乎适用于所有链接实体属性。

<fetch top="500" no-lock="true" >
<entity name="contact" >
    <attribute name="fullname" alias="contactName" />
    <link-entity name="incident" from="customerid" to="contactid" link-type="inner" alias="incident" >
    <attribute name="caseorigincode" alias="caseorigincode" />
    </link-entity>
</entity>
</fetch>

省略链接实体属性别名
如果你省略了链接实体属性的别名,它会返回一个可怕的名称:

<fetch top="500" no-lock="true" >
<entity name="contact" >
    <attribute name="fullname" alias="contactName" />
    <link-entity name="incident" from="customerid" to="contactid" link-type="inner" alias="incident" >
    <attribute name="caseorigincode" />
    </link-entity>
</entity>
</fetch>

返回的对象有这样一个字段:
incident_x002e_caseorigincode@OData.Community.Display.V1.FormattedValue:"WhatsApp"

也许您遇到的问题已在 8.2 版中得到解决。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-19
    • 2019-11-02
    • 2019-04-12
    • 1970-01-01
    • 2021-11-13
    • 1970-01-01
    • 1970-01-01
    • 2020-10-16
    相关资源
    最近更新 更多