Skip to main content

Notifications

Business Central forum
Suggested answer

Update attachment content via API call

Posted on by 30

I am creating an attachment in Business Central and trying to update attachment content in my application by calling PATCH API "">api.businesscentral.dynamics.com/.../attachmentContent"  in my application.

I am reading the attachment file content from blob storage as BinaryData. And, using below code snippet to call PATCH API. The data is updated in attachment in Business Central. However, when we download the attachment file from Business Central, the file is corrupted and unable to open. I am supposed to upload any file formats (like PDF, xlsx, docx, txt, etc).

public HttpResponseMessage UpdatePurchaseInvoiceAttachmentContent(string mediaEditApi, BinaryData binaryData)
{
Console.WriteLine("Method - UpdatePurchaseInvoiceAttachmentContent");
HttpRequestMessage httpRequestMessage = null;
HttpResponseMessage httpResponseMessage = null;
HttpClient httpClient = null;
try
{
httpRequestMessage = new HttpRequestMessage(HttpMethod.Patch, mediaEditApi);
httpClient = GetHttpClientWithAuthHeader();
string contentJson = JsonSerializer.Serialize(binaryData.ToString());
httpRequestMessage.Content = new StringContent(contentJson);
httpRequestMessage.Headers.TryAddWithoutValidation("Content-Type","application/json");
httpRequestMessage.Headers.TryAddWithoutValidation("If-Match", "*");
Task<HttpResponseMessage> response = httpClient.SendAsync(httpRequestMessage);
httpResponseMessage = response.Result;
httpResponseMessage.EnsureSuccessStatusCode();
}
catch(Exception ex)
{
Console.WriteLine(ex);
}
return httpResponseMessage;
}

Any pointers to update attachment content code snippet will be much appreciated.

Categories:
  • Suggested answer
    Marco Mels Profile Picture
    Marco Mels on at
    RE: Update attachment content via API call

    Thank you for sharing final solution.

    Thanks.

  • dynamicsdevman Profile Picture
    dynamicsdevman 30 on at
    RE: Update attachment content via API call

    Finally I made it.. I converted the binary data into stream and assigned StreamContent to HttpRequestMessage.Content. And, it worked fine, able to download and open the file.

    public HttpResponseMessage UpdatePurchaseInvoiceAttachmentContent(string mediaEditApi, BinaryData binaryData)
    {
    Console.WriteLine("Method - UpdatePurchaseInvoiceAttachmentContent");
    HttpRequestMessage httpRequestMessage = null;
    HttpResponseMessage httpResponseMessage = null;
    HttpClient httpClient = null;
    try
    {
    httpRequestMessage = new HttpRequestMessage(HttpMethod.Patch, mediaEditApi);
    httpClient = GetHttpClientWithAuthHeader();
    httpRequestMessage.Content = new StreamContent(binaryData.ToStream());
    httpRequestMessage.Headers.TryAddWithoutValidation("Content-Type","application/json");
    httpRequestMessage.Headers.TryAddWithoutValidation("If-Match", "*");
    Task<HttpResponseMessage> response = httpClient.SendAsync(httpRequestMessage);
    httpResponseMessage = response.Result;
    httpResponseMessage.EnsureSuccessStatusCode();
    }
    catch(Exception ex)
    {
    Console.WriteLine(ex);
    }
    return httpResponseMessage;
    }

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

Anton Venter – Community Spotlight

Kudos to our October Community Star of the month!

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Dynamics 365 Community Newsletter - September 2024

Check out the latest community news

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,524 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,493 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans