web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Suggested Answer

Update attachment content via API call

(0) ShareShare
ReportReport
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.

  • dynamicsdevman Profile Picture
    30 on at

    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;
    }

  • Suggested answer
    Marco Mels Profile Picture
    Microsoft Employee on at

    Thank you for sharing final solution.

    Thanks.

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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the August Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 720 Super User 2026 Season 2

#2
YUN ZHU Profile Picture

YUN ZHU 436 Super User 2026 Season 2

#3
AndrewThomas81 Profile Picture

AndrewThomas81 409 Super User 2026 Season 2

Last 30 days Overall leaderboard

Featured topics

Microsoft Training Manuals

Product updates

Dynamics 365 release plans