从 CRM Web 中的 OData 查询获取选项集文本 api
Get optionset text from OData query in CRM web api
https://example.com/crm/api/data/v8.2/accounts?$select=custom_optionset
以上查询选择了 CRM 中选项集字段中的所有值。 return 数据看起来像这样:
{
{
"@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"
}
}
我不想要选项集的值。我想要关联的文本标签。
我如何获得这个?
要使用 webapi 获取选项集文本,请在请求中使用以下代码段 header。
req.setRequestHeader("Prefer", "odata.include-annotations=OData.Community.Display.V1.FormattedValue");
这将 return 选择列表文本类似于查找 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();
}
如果您使用 Jason Lattimer 的 CRM RESTBuilder 解决方案,它会创建一个类似这样的查询,其中包括 header 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
}
我发现在 CRM 中使用 XrmQuery 调用 Web API 最简单(完全披露:我在开发此软件的公司工作)。无需手动摆弄 headers 和 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
的字段。
上面的代码将 return:
Industry is: Broadcasting Printing and Publishing
https://example.com/crm/api/data/v8.2/accounts?$select=custom_optionset
以上查询选择了 CRM 中选项集字段中的所有值。 return 数据看起来像这样:
{
{
"@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"
}
}
我不想要选项集的值。我想要关联的文本标签。 我如何获得这个?
要使用 webapi 获取选项集文本,请在请求中使用以下代码段 header。
req.setRequestHeader("Prefer", "odata.include-annotations=OData.Community.Display.V1.FormattedValue");
这将 return 选择列表文本类似于查找 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();
}
如果您使用 Jason Lattimer 的 CRM RESTBuilder 解决方案,它会创建一个类似这样的查询,其中包括 header 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
}
我发现在 CRM 中使用 XrmQuery 调用 Web API 最简单(完全披露:我在开发此软件的公司工作)。无需手动摆弄 headers 和 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
的字段。
上面的代码将 return:
Industry is: Broadcasting Printing and Publishing