zoukankan      html  css  js  c++  java
  • Dynamics CRM2016 Web API获取实体元数据Picklist属性的Text&Value

             通过组织服务中获取实体picklist字段的text和value可以通过RetrieveAttributeRequest实现,但在使用web api的今天该怎么实现,本文即来一探究竟,本篇基于SDK中的Query Metadata using the Web API一节。

         首先我们要获取到实体的metadataid,代码如下,这个metadataid后面会用到,我这里用的是account实体

     string weburi = WebUri + "EntityDefinitions?$filter=LogicalName eq 'account'";
            string result = "";
            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");
            using (HttpWebResponse res = (HttpWebResponse)req.GetResponse())
            {
                StreamReader read = new StreamReader(res.GetResponseStream());
                result = read.ReadToEnd();
            }
            JObject jobject = JsonConvert.DeserializeObject<JObject>(result);
            string id = jobject["value"][0]["MetadataId"].ToString();

          获取到了实体的metadataid后就去查询具体的这个实体下面的元数据了,代码如下(根据value值查对应的text),注意代码中有段if-else,即GlobalOptionSet和OptionSet,区别是你的这个picklist是否用到了全局选项集,

     string weburi = WebUri + +"EntityDefinitions("+ metadataid + ")/Attributes/Microsoft.Dynamics.CRM.PicklistAttributeMetadata?$select=LogicalName&$expand=OptionSet,GlobalOptionSet"; ;
            string result = "";
            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");
            using (HttpWebResponse res = (HttpWebResponse)req.GetResponse())
            {
                StreamReader read = new StreamReader(res.GetResponseStream());
                result = read.ReadToEnd();
            }
            JObject jobject = JsonConvert.DeserializeObject<JObject>(result);
            JArray ja = (JArray)jobject["value"];
            string text = "";
            if (ja.Count > 0)
            {
                for (int i = 0; i < ja.Count; i++)
                {
                    if (ja[i]["LogicalName"].ToString() == "new_testoption")
                    {
                        if (ja[i]["OptionSet"].ToString() != "")
                        {
                            ja = JsonConvert.DeserializeObject<JArray>(ja[i]["OptionSet"]["Options"].ToString());
                            for (int j = 0; j < ja.Count; j++)
                            {
                                if (ja[j]["Value"].ToString() == "100000000")
                                {
                                    ja = JsonConvert.DeserializeObject<JArray>(ja[j]["Label"]["LocalizedLabels"].ToString());
                                    text = ja[0]["Label"].ToString();
                                }
                            }
                        }
                        else
                        {
                            ja = JsonConvert.DeserializeObject<JArray>(ja[i]["GlobalOptionSet"]["Options"].ToString());
                            for (int j = 0; j < ja.Count; j++)
                            {
                                if (ja[j]["Value"].ToString() == "0")
                                {
                                    ja = JsonConvert.DeserializeObject<JArray>(ja[j]["Label"]["LocalizedLabels"].ToString());
                                    text = ja[0]["Label"].ToString();
                                }
                            }
                        }
                    }
                }
            }
          这里的json处理用的是newtonsoft,如果要引用上述代码记得添加newtonsoft的引用,上述代码是根据picklist的value值去查找对应的text的,反之亦然。

         

  • 相关阅读:
    EasyARM-iMX283A的Linux 开发环境构建
    linux指令tar笔记
    使用cuteFTP与虚拟机交互文件---安装ftp服务
    SecureCRT显示乱码的解决办法
    【转】简明 Vim 练级攻略
    图像识别___YUV学习手记
    一个简易的软件定时器
    OV7670配置和调试小结
    linux驱动开发( 五) 字符设备驱动框架的填充file_operations结构体中的操作函数(read write llseek unlocked_ioctl)
    hash-1.hash表和hash算法
  • 原文地址:https://www.cnblogs.com/cl1024cl/p/6205781.html
Copyright © 2011-2022 走看看