之前有篇博文介绍了如何获取查找字段的name值(跳转),本篇在此基础上再延伸下,实现的效果类似于EntityReference,可以取到查找字段的id,name,localname。
这里我以客户实体为例,定义了一个叫new_city的查找字段,现在获取它的值
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("http://xx/api/data/v8.0/accounts(1D539D5B-D8B2-E611-80EB-C255C0417B60)
?$select=_new_cityid_value");
req.Credentials = new NetworkCredential(name, 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=*");
using (HttpWebResponse res = (HttpWebResponse)req.GetResponse())
{
StreamReader read = new StreamReader(res.GetResponseStream());
string result = read.ReadToEnd();
}
从上面的代码可以看到,和之前的博文中的唯一区别在于head中的prefer,我们把OData.Community.Display.V1.FormattedValue替换成了*。
我们来看看返回的json值,查找字段相关的信息都出来了。
{
"@odata.context":"http://xx/api/data/v8.0/$metadata#accounts(_new_cityid_value)/$entity","@odata.etag":"W/\"1421510\"",
"_new_cityid_value@Microsoft.Dynamics.CRM.associatednavigationproperty":"new_cityid",
"[email protected]":"new_city",
"[email protected]":"\u4e0a\u6d77",
"_new_cityid_value":"41017df2-2b7b-e611-9424-952f64877d17",
"accountid":"1d539d5b-d8b2-e611-80eb-c255c0417b60"
}