Dynamics CRM 2015/2016 Web API:基于视图的数据查询

Posted Jeff Xiong

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Dynamics CRM 2015/2016 Web API:基于视图的数据查询相关的知识,希望对你有一定的参考价值。

Dynamics CRM 2016 Web API支持基于视图来查询数据,博主初看到该Feature,着实让我眼前一亮,这个功能太给力了,完全颠覆了之前复杂繁琐的取数方式了,现在的视图真正到了定义一次,处处复用的地步了!使用方法也很简单,只需要加个参数并指定需要调用的View ID就好了。接下来我给大家演示3个比较常用的场景:调用公共视图,调用私有视图,调用SubGrid视图。

公共视图

什么是公共视图?就是大家都有权限访问的视图,比如客户实体上的我的可用客户视图,我们一般在UI上面这么调用它们


调用代码

            HttpRequestMessage getPredefinedQueryIdReq = new HttpRequestMessage(HttpMethod.Get, webApiUrl + "/savedqueries?$filter=name eq 'My Active Accounts'");
            getPredefinedQueryIdReq.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessCode);

            HttpResponseMessage getPredefinedQueryIdResp = await client.SendAsync(getPredefinedQueryIdReq);
            if (getPredefinedQueryIdResp.IsSuccessStatusCode)
            
                JObject result = JsonConvert.DeserializeObject<JObject>(await getPredefinedQueryIdResp.Content.ReadAsStringAsync());
                string queryId = result["value"][0]["savedqueryid"].Value<string>();

                HttpRequestMessage getAccByQueryViewReq = new HttpRequestMessage(HttpMethod.Get, webApiUrl + "/accounts?savedQuery=" + queryId);
                getAccByQueryViewReq.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessCode);

                HttpResponseMessage getAccByQueryViewResp = await client.SendAsync(getAccByQueryViewReq);
                if (getAccByQueryViewResp.IsSuccessStatusCode)
                
                    JObject result2 = JsonConvert.DeserializeObject<JObject>(await getAccByQueryViewResp.Content.ReadAsStringAsync());
                    Console.WriteLine(result2.ToString());

                
            


私有视图

私有视图就是自己定义的视图,专为自己使用,我们一般在UI上面这么调用它们

调用代码

            HttpRequestMessage getUserQueryIdReq = new HttpRequestMessage(HttpMethod.Get, webApiUrl + "/userqueries?$filter=name eq 'Jeffs View'");
            getUserQueryIdReq.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessCode);

            HttpResponseMessage getUserQueryIdResp = await client.SendAsync(getUserQueryIdReq);
            if (getUserQueryIdResp.IsSuccessStatusCode)
            
                JObject result = JsonConvert.DeserializeObject<JObject>(await getUserQueryIdResp.Content.ReadAsStringAsync());
                string queryId = result["value"][0]["userqueryid"].Value<string>();

                HttpRequestMessage getAccByQueryViewReq = new HttpRequestMessage(HttpMethod.Get, webApiUrl + "/accounts?userQuery=" + queryId);
                getAccByQueryViewReq.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessCode);

                HttpResponseMessage getAccByQueryViewResp = await client.SendAsync(getAccByQueryViewReq);
                if (getAccByQueryViewResp.IsSuccessStatusCode)
                
                    JObject result2 = JsonConvert.DeserializeObject<JObject>(await getAccByQueryViewResp.Content.ReadAsStringAsync());
                    Console.WriteLine(result2.ToString());

                
            


SubGrid视图

级联视图,根据数据的上下文环境进行数据筛选的视图,我们一般在UI上面这么调用它们


调用代码

  HttpRequestMessage getDataBySubGridViewReq = new HttpRequestMessage(HttpMethod.Get, webApiUrl + "/accounts(823ef58a-75bb-e511-80d9-c4346bc43f3c)/contact_customer_accounts/?savedQuery=00000000-0000-0000-00aa-000010001033");
            getDataBySubGridViewReq.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessCode);

            HttpResponseMessage getDataBySubGridViewResp = await client.SendAsync(getDataBySubGridViewReq);
            if (getDataBySubGridViewResp.IsSuccessStatusCode)
            
                JObject result = JsonConvert.DeserializeObject<JObject>(await getDataBySubGridViewResp.Content.ReadAsStringAsync());
                Console.WriteLine(result.ToString());
            


以上是关于Dynamics CRM 2015/2016 Web API:基于视图的数据查询的主要内容,如果未能解决你的问题,请参考以下文章

Dynamics CRM 2015/2016/365 Web API:级联查询

Dynamics CRM 2015/2016 Web API:基于视图的数据查询

Dynamics CRM 2015/2016 Web API:新的数据查询方式

Dynamics CRM 2015/2016 Web API:聚合查询

Dynamics CRM 2015/2016 Web API:Unbound Action 和 Bound Action

Dynamics CRM 2015/2016 Web API:聚合查询