Dynamics 365 CRM online的WebAPI查询使用count=true生成下一个查询链接,实现分批查询超出5000记录的数据集
Dynamics 365 CRM online的WebAPI查询使用count=true生成下一个查询链接,实现分批查询超出5000记录的数据集一、Dynamics 365 CRM online的WebAPI查询使用count=true生成下一个查询链接,实现分批查询超出5000记录的数据集二、使用步骤1.引入库using System;using System.Collections.Gener
·
- Dynamics 365 CRM online的WebAPI查询使用count=true生成下一个查询链接,实现分批查询超出5000记录的数据集
一、Dynamics 365 CRM online的WebAPI查询使用count=true生成下一个查询链接,实现分批查询超出5000记录的数据集
二、使用步骤
1.引入库
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Http;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Web;
2.直接贴上如下的测试成功的代码
public HttpClient connectToCRM()
{
HttpClient httpClient = new HttpClient();
System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
AuthenticationContext authContext = new AuthenticationContext(tokenUrl, false);
AuthenticationResult result = authContext.AcquireToken(resource, clientId, new UserCredential(username, pwd));
httpClient.BaseAddress = new Uri(ServiceUrl);
httpClient.Timeout = new TimeSpan(0, 2, 0);
httpClient.DefaultRequestHeaders.Add("OData-MaxVersion", "4.0");
httpClient.DefaultRequestHeaders.Add("OData-Version", "4.0");
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken);
httpClient.DefaultRequestHeaders.Add("Prefer", "odata.include- annotations=\"Microsoft.Dynamics.CRM.fetchxmlpagingcookie\"");
return httpClient;
}
public Task<HttpResponseMessage> getCRMData(string queryString, HttpClient httpClient, bool isNextLink = false)
{
Task<HttpResponseMessage> data = null;
string OdataRequest = null;
if (isNextLink)
{
OdataRequest = queryString;
}
else
{
OdataRequest = ServiceUrl + queryString;
}
try
{
//Task<System.IO.Stream> st = httpClient.GetStreamAsync(OdataRequest);
data = httpClient.GetAsync(OdataRequest);
}
catch (Exception ex)
{
throw ex;
}
return data;
}
private string MetaDataQuerystring()
{
string querystring = "salesorders? $select=salesorderid,thk_doc_no,invoice_no,datefulfilled,country,type,statecode,billto_country," + "&$expand=order_details($select=salesorderid,doc_no,productid,quantity,priceperunit),new_salesorder_salesperson($select=salespersonid)" +"&$filter=modifiedon ge '2020-11-11 0:00:00.000' and modifiedon lt '2020-11-11 13:59:59' &$orderby=modifiedon&$count=true ";
return querystring;
}
public void getCrmJson()
{
try
{
int pagingnumber = 1;
int totalcount = 0;
httpClient = crm.connectToCRM();
bool hasNextLink = false;
if (httpClient != null)
{
string curXml = this.MetaDataQuerystring();
while (true)
{
JObject _jobj = new JObject();
_jobj = crm.convertToJson(crm.getCRMData(curXml, httpClient, hasNextLink));
List<string> l = CRMModify.GetJObjectListValue(_jobj, "invoice_no");
for (int i = 0; i < l.Count; i++)
{
Console.WriteLine(i.ToString() + " -> " + l[i].ToString());
totalcount++;
}
if (_jobj.ContainsKey("@odata.nextLink"))
{
pagingnumber++;
curXml = _jobj["@odata.nextLink"].ToString();
hasNextLink = true;
}
else
{
break;
}
}
Console.WriteLine("total pages : " + pagingnumber.ToString() + " total records: " + totalcount.ToString());
System.Threading.Thread.Sleep(10000);
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
httpClient.Dispose();
}
}

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐
所有评论(0)