Dynamics CRM 2015/2016新特性之二十七:使用Web API查询元数据

关注本人微信和易信公众号: 微软动态CRM专家罗勇 ,回复210或者20160321可方便获取本文,同时可以在第一间得到我发布的最新的博文信息,follow me!
通过Web API查询元数据和查询记录差不多,不同的是URL部分变化了,总是会返回的是 MetadataId 属性及其值,而且部分操作符好像不支持,发现top,contains 等操作符不支持。
我们先来看一个实体的元数据吧,我这里使用的URL是 http://lycrmvm.cloudapp.net:5555/Demo/api/data/v8.0/EntityDefinitions?$filter=SchemaName eq 'ly_Test' ,浏览器中打开效果如下,当然我这里是显示我自己创建的实体的元数据,不是所有的实体,我在URL中加了筛选条件。

  

下面的示例代码就是查询一个实体的元数据信息:
var clientURL = Xrm.Page.context.getClientUrl();
var req = new XMLHttpRequest()
req.open("GET", encodeURI(clientURL + "/api/data/v8.0/EntityDefinitions?$filter=SchemaName eq 'ly_Test'"), 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.onreadystatechange = function () {
    if (this.readyState == 4) {
        req.onreadystatechange = null;
        if (this.status == 200) {
            var responseJSON = JSON.parse(this.responseText);
            Xrm.Utility.alertDialog("查询到的元数据ID是" + responseJSON.value[0]["MetadataId"] + ",实体复数显示名称是:" + responseJSON.value[0].DisplayCollectionName.LocalizedLabels[0].Label + ",主属性是:" + responseJSON.value[0].PrimaryNameAttribute);
        }
        else {
            var error = JSON.parse(this.responseText).error;
            Xrm.Utility.alertDialog("查询罗勇测试实体元数据出错." + error.message);
        }
    }
};
req.send();

 

 
下面是结果截图:

 

 

 
当然也可支持count操作符,我这里的例子如下:
var clientURL = Xrm.Page.context.getClientUrl();
var req = new XMLHttpRequest()
req.open("GET", encodeURI(clientURL + "/api/data/v9.1/EntityDefinitions?$select=DisplayCollectionName,PrimaryNameAttribute,SchemaName&$filter=IsCustomizable/Value eq true&$count=true"), 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.onreadystatechange = function () {
    if (this.readyState == 4) {
        req.onreadystatechange = null;
        if (this.status == 200) {
            var responseJSON = JSON.parse(this.responseText);
            Xrm.Utility.alertDialog("共有" + responseJSON["@odata.count"] + "个实体可以定制,第一个实体的元数据ID是" + responseJSON.value[0]["MetadataId"] + ",实体复数显示名称是:" + responseJSON.value[0].DisplayCollectionName.LocalizedLabels[0].Label + ",架构名称是:" + responseJSON.value[0].SchemaName + ",主属性是:" + responseJSON.value[0].PrimaryNameAttribute);
        }
        else {
            var error = JSON.parse(this.responseText).error;
            Xrm.Utility.alertDialog("查询罗勇测试实体元数据出错." + error.message);
        }
    }
};
req.send();

 

还有个常用的就是根据一个实体的MetadatdId来查看实体的字段信息,我这里也上一个例子,注意查看URL的写法,更多的请参考SDK的 Query Metadata using the Web API 章节:
var clientURL = Xrm.Page.context.getClientUrl();
var req = new XMLHttpRequest()
req.open("GET", encodeURI(clientURL + "/api/data/v8.1/EntityDefinitions(e373eab4-9ca4-e511-80cc-000d3a80ce7f)?$select=SchemaName&$expand=Attributes($select=SchemaName;$filter=AttributeType eq Microsoft.Dynamics.CRM.AttributeTypeCode'String' and IsCustomizable/Value eq true and IsCustomAttribute eq true and IsLogical eq false)"), 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.onreadystatechange = function () {
    if (this.readyState == 4) {
        req.onreadystatechange = null;
        if (this.status == 200) {
            var stringFields = "";
            var responseJSON = JSON.parse(this.responseText);
            if (responseJSON.Attributes != null && responseJSON.Attributes.length >= 1) {
                for (var i = 0; i < responseJSON.Attributes.length; i++) {
                    stringFields += responseJSON.Attributes[i].SchemaName + ";";
                }
            }
            if (stringFields != "") {
                Xrm.Utility.alertDialog("这个实体的文本字段有" + stringFields.substring(0, stringFields.length - 1));
            }
        }
        else {
            var error = JSON.parse(this.responseText).error;
            Xrm.Utility.alertDialog("查询罗勇测试实体元数据出错." + error.message);
        }
    }
};
req.send();

 

如果是根据LogicalName来查看实体信息,有更加简单的写法,类似如下,但是你如果将下面url中的LogicalName改成SchemaName则会报错。

https://demo.luoyong.me/api/data/v9.1/EntityDefinitions(LogicalName='account')

结果截图如下:

 

很多时候还需要根据实体的ObjectTypeCode来查找究竟是哪个实体,用这个也可以查询出来的:

https://demo.luoyong.me/api/data/v9.1/EntityDefinitions?$select=SchemaName&$filter=ObjectTypeCode eq 1
 
大家可能还会发现,返回的中文都是编码的,如何看到中文的呢?这里用chrome来说明,按F12以后点击 Network ,在Name这里点击下,点击右边的 Preview 这个tab,这里面显示的就是中文了。
  
还可以通过stringmap实体来查看选项集的相关信息,使用的url如下:
https://demo.luoyong.me/api/data/v9.1/stringmaps?$select=attributevalue,value,langid&$filter=objecttypecode eq 'ly_test' and attributename eq 'ly_optionset' and langid eq 1033
 
比stringmap更靠谱获取optionset信息的是官方的例子(Example: Retrieve metadata items by name),比如:
https://demo.luoyong.me/api/data/v9.1/EntityDefinitions(LogicalName='account')/Attributes(LogicalName='accountcategorycode')/Microsoft.Dynamics.CRM.PicklistAttributeMetadata?$select=LogicalName&$expand=OptionSet($select=Options),GlobalOptionSet($select=Options)
 
posted @ 2020-06-14 12:21  微软MVP(15-18)罗勇  阅读(575)  评论(0编辑  收藏  举报