Skip to main content

Notifications

Announcements

No record found.

Dynamics 365 general forum

Technical - Executing Web Api Batch request via C#

Posted on by Microsoft Employee

Hi,

** Apologies in advance if I have posted in a non-technical category, I couldn't find the development categories. **

Having difficulties executing batch request using CRM Web Api. has anyone got any sample code that could assist me in this.

What I have done?

I have completed Authentication, Create, Update, Retrieve and Delete operations successfully however, batch requests seems to have pushed me back. Also, there isn't much samples available online.

I had followed this post http://gtezini.blogspot.com/2017/08/dynamics-365-web-api-batch-operation-in.html, however, HttpMessageContent is no longer available to use or must be part of another assembly i.e. not System.Net.Http.

Any help would be appreciated.

Thanks and Regards,

Darrel 

Categories:
  • BiR Profile Picture
    BiR 180 on at
    RE: Technical - Executing Web Api Batch request via C#

    Has this been solved? Facing the same issue, any help will be greatly appreciated.

    I keep getting the following error.

    The 'Content-Type' header is missing. The 'Content-Type' header must be specified for each MIME part of a batch message.","ExceptionMessage":"The 'Content-Type' header is missing. The 'Content-Type' header must be specified for each MIME part of a batch message."

    The code looks like:

     var batchId = Guid.NewGuid().ToString();
                            log.LogInformation($"Batch Request Id = {batchId}.");
                            HttpRequestMessage deleteBatchRequestMessage = new HttpRequestMessage(HttpMethod.Post, "$batch");
                            deleteBatchRequestMessage.Content = new MultipartContent("mixed", "batch_" + batchId);
    
    HttpClient client = new HttpClient();
                            client.BaseAddress = new Uri(d365Url);
                            // Default Request Headers needed to be added in the HttpClient Object
                            client.DefaultRequestHeaders.Add("OData-MaxVersion", "4.0");
                            client.DefaultRequestHeaders.Add("OData-Version", "4.0");
                            client.DefaultRequestHeaders.Add("Prefer", "odata.include-annotations=\"OData.Community.Display.V1.FormattedValue\"");
                            d365HttpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                            // Set the Authorization header with the Access Token received specifying the Credentials
                            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", d365Token);
    
                            HttpResponseMessage response = await client.SendAsync(deleteBatchRequestMessage);
                            var ass = await response.Content.ReadAsStringAsync();


  • David Jennaway Profile Picture
    David Jennaway 14,063 on at
    RE: Technical - Executing Web Api Batch request via C#

    Can you post the code you are using, and any errors you get, and when ?

  • Suggested answer
    Abby Kong Profile Picture
    Abby Kong 6 on at
    RE: Technical - Executing Web Api Batch request via C#

    Hi Darrel,

    HttpMessageContent is not part of the main System.Net, it's a class in assembly System.Net.Http.Formatting under Asp.Net Web API. Try to install this package, it should resolve the referencing issue for HttpMessageContent :

    https://www.nuget.org/packages/Microsoft.AspNet.WebApi.Client/

    Regards,

    Abby

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans