如何在odata的getdata中找到optionset标签?

时间:2016-08-16 08:49:51

标签: asp.net-ajax dynamics-crm dynamics-crm-2013

Label of optionset中使用odata查询检索entitydata时,我想要ajax而不是值。

var oDataUri = serverUrl + "/XRMServices/2011/OrganizationData.svc/ProductSet";

$.ajax({
   type: "GET",
   contentType: "application/json; charset=utf-8",
   datatype: "json",
   url: oDataUri,
   async: false,
   beforeSend: function (XMLHttpRequest) {
        XMLHttpRequest.setRequestHeader("Accept", "application/json");
   },
   success: function (msg, textStatus, XmlHttpRequest) {
        debugger;
        var data = msg.d;
        StateCode = data.StateCode.Value;
        ProductStructure = data.ProductStructure.Value;    
   },
   error: function (XMLHttpRequest, textStatus, errorThrown) {
        alert();
   }
});

有人可以提出解决方法吗?

2 个答案:

答案 0 :(得分:0)

OData查询不返回包含选项集标签的属性元数据,只返回选项集值。

您必须进行额外调用才能获取选项集元数据并获取选项设置值的匹配标签。使用SDK.Metadata.js

答案 1 :(得分:0)

我发现了一种不使用下面的sdk定义的方法。

serverUrl = location.protocol + "//" + location.host;
var Query = "products";
var req = new XMLHttpRequest();
req.open("GET", serverUrl + "/api/data/v8.0/" + Query, false);
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) {
            data = JSON.parse(this.response);
            if (data != null) {

                alert(data.value[0]["statuscode@OData.Community.Display.V1.FormattedValue"]); //for optionset text

            }

        } 
        else {
            var error = JSON.parse(this.response).error;
            alert(error.message);
        }
    }
};
req.send();