zoukankan      html  css  js  c++  java
  • 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/v8.0/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.0/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();
    结果截图如下:
    很多时候还需要根据实体的ObjectTypeCode来查找究竟是哪个实体,用这个也可以查询出来的:
    https://demo.luoyong.me/api/data/v8.1/EntityDefinitions?$select=SchemaName&$filter=ObjectTypeCode eq 1
     
    大家可能还会发现,返回的中文都是编码的,如何看到中文的呢?这里用chrome来说明,按F12以后点击 Network ,在Name这里点击下,点击右边的 Preview 这个tab,这里面显示的就是中文了。
     
     
    还可以通过stringmap实体来查看选项集的相关信息,使用的url如下:
    https://demo.luoyong.me/api/data/v8.1/stringmaps?$select=attributevalue,value&$filter=objecttypecode eq 'ly_test' and attributename eq 'ly_optionset'
     
  • 相关阅读:
    C++中typename关键字的用法
    多项式系数的值
    记录几个经典的字符串hash算法
    linux timerfd系列函数总结
    linux 获取网络状态信息(Rtnetlink)
    linux netlink通信机制
    linux进程、线程与cpu的亲和性(affinity)
    C语言检查ip是否合法
    使用libpcap获取http报文
    使用libpcap过滤arp
  • 原文地址:https://www.cnblogs.com/luoyong0201/p/Dynamics_365_Web_API_Query_Metadata.html
Copyright © 2011-2022 走看看