A vantagem deste tipo de operação é que podemos, em um único POST HTTP, enviar um LOTE de registros para serem, por exemplo, criados no CRM.
Abaixo segue trecho de código que desenvolvi para inserir dois Contatos (CTT1, CTT2) no Dynamics 365. O exemplo está inserido em um projeto do tipo ASP.NET MVC - WEB API.
[HttpGet]
public void CreateDyn365BatchContacts()
{
ConnectToCRM(null);
Task.WaitAll(Task.Run(async () => await RunBatchAsync()));
}
public async Task RunBatchAsync() { await getWebAPIVersion(); webApiURI = config.ServiceUrl + "api/data/" + getVersionedWebAPIPath() + "$batch"; var batchId = Guid.NewGuid().ToString(); var changeSetId = "BBB456"; var contact1 = new JObject(); var contact2 = new JObject(); contact1.Add("firstname", "CTT"); contact1.Add("lastname", "1"); contact2.Add("firstname", "CTT"); contact2.Add("lastname", "2"); var batchContent = new MultipartContent("mixed", "batch_" + batchId); // CTT1 var changeSetContent = new MultipartContent("mixed", "changeset_" + changeSetId); var httpRM1 = new HttpRequestMessage(HttpMethod.Post, config.ServiceUrl + "api/data/" + getVersionedWebAPIPath() + "contacts"); httpRM1.Content = new StringContent(JsonConvert.SerializeObject(contact1), Encoding.UTF8, "application/json"); var httpMC1 = new HttpMessageContent(httpRM1); httpMC1.Headers.Remove("Content-Type"); httpMC1.Headers.Add("Content-Type", "application/http"); httpMC1.Headers.Add("Content-Transfer-Encoding", "binary"); httpMC1.Headers.Add("Content-ID", "1"); httpMC1.Headers.Add("OData-MaxVersion", "4.0"); httpMC1.Headers.Add("OData-Version", "4.0"); changeSetContent.Add(httpMC1); // CTT2 var httpRM2 = new HttpRequestMessage(HttpMethod.Post, config.ServiceUrl + "api/data/" + getVersionedWebAPIPath() + "contacts"); httpRM2.Content = new StringContent(JsonConvert.SerializeObject(contact2), Encoding.UTF8, "application/json"); var httpMC2 = new HttpMessageContent(httpRM2); httpMC2.Headers.Remove("Content-Type"); httpMC2.Headers.Add("Content-Type", "application/http"); httpMC2.Headers.Add("Content-Transfer-Encoding", "binary"); httpMC2.Headers.Add("Content-ID", "2"); httpMC2.Headers.Add("OData-MaxVersion", "4.0"); httpMC2.Headers.Add("OData-Version", "4.0"); changeSetContent.Add(httpMC2); batchContent.Add(changeSetContent); var response = SendCrmRequestAsync(HttpMethod.Post, webApiURI, batchContent).Result; var text = await response.Content.ReadAsStringAsync(); }
private async Task<HttpResponseMessage> SendCrmRequestAsync(HttpMethod method, string query, MultipartContent mpContent = null, Boolean formatted = false, int maxPageSize = 10)
{
var request = new HttpRequestMessage();
request.Method = method;
request.RequestUri = new Uri(query);
request.Headers.Add("Prefer", "odata.maxpagesize=" + maxPageSize.ToString());
if (formatted)
request.Headers.Add("Prefer", "odata.include-annotations=OData.Community.Display.V1.FormattedValue");
if (mpContent != null) {
request.Content = mpContent;
}
return await httpClient.SendAsync(request);
}
O código fonte completo está disponnível no zip CrmWebApi_JavaScript_CSharp.rar