https://example.com/crm/api/data/v8.2/accounts?$select=custom_optionset
上述查询选择CRM中选项集字段中的所有值。返回数据如下所示:
{
{
"@odata.etag":"W/\"112607639\"","custom_optionset":285960000,"accountid":"a08f0bd1-e2c4-e111-8c9a-00155d0aa573"
},
{
"@odata.etag":"W/\"112615384\"","custom_optionset":285960010,"accountid":"a18f0bd1-e2c4-e111-8c9a-00155d0aa573"
}
}
我不想要选项集的值。我想要相关的文字标签。 我怎么得到这个?
答案 0 :(得分:4)
要使用webapi获取选项集文本,请在请求标题中使用以下代码段。
req.setRequestHeader("Prefer", "odata.include-annotations=OData.Community.Display.V1.FormattedValue");
这将返回类似于查找FormattedValue的选项列表文本。
整个代码示例:
function retrieveEntity(entityName, Id, columnSet) {
var serverURL = Xrm.Page.context.getClientUrl();
var Query = entityName + "(" + Id + ")" + columnSet;
var req = new XMLHttpRequest();
req.open("GET", serverURL + "/api/data/v8.2/" + Query, true);
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Prefer", "odata.include-annotations=OData.Community.Display.V1.FormattedValue");
req.onreadystatechange = function() {
if (this.readyState == 4 /* complete */ ) {
req.onreadystatechange = null;
if (this.status == 200) {
var data = JSON.parse(this.response);
if (data != null {
alert(data["_primarycontactid_value@OData.Community.Display.V1.FormattedValue"]); //for lookup text
alert(data["paymenttermscode@OData.Community.Display.V1.FormattedValue"]); //for optionset text
}
} else {
var error = JSON.parse(this.response).error;
alert(error.message);
}
}
};
req.send();
}
答案 1 :(得分:3)
我发现最简单的方法是使用XrmQuery来调用CRM中的Web API(完全披露:我为开发此项目的公司工作)。不需要手动摆弄标题和XMLHttpRequests,您可以获得对您自己的CRM中的实体和字段的完全强类型支持。
例如,检索特定帐户并选择industrycode
字段如下所示:
XrmQuery.retrieve(a => a.accounts, "5B86C6EB-DE6D-E611-80DF-C4346BADF080")
.select(a => [a.industrycode])
.includeFormattedValues()
.execute(a => {
console.log("Industry is: " + a.industrycode_formatted);
});
自动添加.includeFormattedValues()
可让您访问名为industrycode_formatted
的字段。
上面的代码将返回:
行业是:广播印刷和出版
答案 2 :(得分:2)
如果您使用Jason Lattimer的CRM RESTBuilder解决方案,它会创建一个这样的查询,其中包含标题req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
:
var req = new XMLHttpRequest();
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/accounts(674D7FDC-47AE-E711-8108-5065F38A3BA1)?$select=accountid,industrycode", 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 accountid = result["accountid"];
var industrycode = result["industrycode"];
var industrycode_formatted = result["industrycode@OData.Community.Display.V1.FormattedValue"];
} else {
Xrm.Utility.alertDialog(this.statusText);
}
}
};
req.send();
结果包括选项集标签:
{
@odata.context:"https://myorg.crm.dynamics.com/api/data/v8.2/$metadata#accounts(accountid,industrycode)/$entity",
@odata.etag:"W/"1959756"",
accountid:"674d7fdc-47ae-e711-8108-5065f38a3ba1",
industrycode@OData.Community.Display.V1.FormattedValue:"Accounting",
industrycode:1
}