Dynamics CRM2016 Web API之获取查找字段的text及选项集的text

时间:2022-03-24 10:41:37

      本篇再来介绍个web api的功能,关于lookup的text这里只是略带,因为有expand,现有的web api就能实现,主要提的是选项集的text,我们通过基本的查询api查出来的字段值只带有value,如果想要获取text也很简单,只需要在request的头部加一条信息即可,下面分别给出了C#及JS的示例代码。

 HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(weburi);
req.Credentials = new NetworkCredential(username, pwd, domain);
req.Method = "Get";
req.Accept = "application/json";
req.ContentType = "application/json; charset=utf-8";
req.Headers.Set("OData-MaxVersion", "4.0");
req.Headers.Set("OData-Version", "4.0");
req.Headers.Set("Prefer", "odata.include-annotations=OData.Community.Display.V1.FormattedValue");
using (HttpWebResponse res = (HttpWebResponse)req.GetResponse())
{
StreamReader read = new StreamReader(res.GetResponseStream());
result = read.ReadToEnd();
}

 var req = new XMLHttpRequest()    req.open("get", url);    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) {            if (this.status == 200) {                successCallback(JSON.parse(this.responseText));            }            else {                errorCallback(JSON.parse(this.responseText).error.message);            }        }    };    req.send();

      从下面的截图就能看到,text值已查询出。

Dynamics CRM2016 Web API之获取查找字段的text及选项集的text