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的,反之亦然。

         

  • 相关阅读:
    2015年5月1日 转载--各种变量在内存中的分布
    2015年4月30日 计算两个日期天数,写的代码
    2015年4月29日 dayofweek
    2015年4月28日
    2015年4月28日----高大上的数组,进制准换,最多是35进制
    2015年4月27日---C语言:输出特殊图案,请在c环境中运行,看一看,Very Beautiful!
    2015年4月27日
    《C++ Primer》学习笔记:迭代器介绍
    《C++ Primer》学习笔记:3.3.3其他vector操作
    《C++ Primer》学习笔记:向vector对象添加元素蕴含的编程假定
  • 原文地址:https://www.cnblogs.com/cl1024cl/p/6205781.html
Copyright © 2011-2022 走看看