CRM 2016 web api 与 C#

标签: Asp.net C#
发布时间: 2017/2/25 3:18:56
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我们有 CRM 2016 前提,并且想要消耗使用 c# 的 API:

var credentials = new NetworkCredential(username, password);
HttpClient client = new HttpClient(new HttpClientHandler() {Credentials = credentials});
client.BaseAddress = new Uri("https://xxx.elluciancrmrecruit.com/api/data/v8.0/datatel_events");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = client.GetAsync("datatel_events?$orderby=datatel_eventname").Result;
if (response.IsSuccessStatusCode)
{
   var yourcustomobjects = response.Content.ReadAsStringAsync();
}
else
{
   //Something has gone wrong, handle it here
}

但它始终返回 HTML 页面说尝试以后而不是 json 响应。

解决方法 1:

你应该查询 OData 端点获取信息您正在寻找假设 date1_events 是一个自定义的实体 (虽然命名约定似乎有了)。

作为一个例子查询 contacts :

var credentials = new NetworkCredential(username, password);
var client = new HttpClient(new HttpClientHandler() {Credentials = credentials})
{
    BaseAddress = new Uri("https://xxx.elluciancrmrecruit.com/api/data/v8.0/")
};
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var response = client.GetAsync("accounts?$top=1").Result;
if (response.IsSuccessStatusCode)
{
    var yourcustomobjects = response.Content.ReadAsStringAsync();
}
官方微信
官方QQ群
31647020