下面是一些代码,它们使用 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
}]
}
它适用于内部和外部链接类型。
链接实体属性别名与属性名称相同
如果使链接实体属性的别名与属性名称相同,则根本不会返回该属性。此问题似乎适用于所有链接实体属性。
<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 版中得到解决。