从CRM Web API中的OData查询获取选项集文本 [英] Get optionset text from OData query in CRM web api

查看:70
本文介绍了从CRM Web API中的OData查询获取选项集文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

https://example.com/crm/api/data/v8.2/accounts?$select=custom_optionset

上面的查询选择CRM中选项集字段中的所有值.返回数据如下所示:

The above query selects all values in an optionset field in CRM. The return data looks something like this:

{
    {
        "@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"
    }
}

我不想要选项集的值.我想要关联的文本标签.我怎么得到这个?

I don't want the value of the optionset. I want the associated text label. How do I get this?

推荐答案

要使用webapi获取选项集文本,请在请求标头中使用以下代码段.

To get optionset text using webapi, use below snippet in request header.

req.setRequestHeader("Prefer", "odata.include-annotations=OData.Community.Display.V1.FormattedValue");

这将返回类似于查找FormattedValue的选择列表文本.

This will return the picklist text similar to lookup 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();
    }

参考.

这篇关于从CRM Web API中的OData查询获取选项集文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆