Business Central Attachments with standard API – Part 1 Basic Explanation
A nice feature which is explained fairly well in the Microsoft docs site but it is especially nice to have a working example. (https://docs.microsoft.com/en-us/dynamics-nav/api-reference/v1.0/api/dynamics_attachment_patch). The attachments API itself actually points to the “Incoming Documents” feature of BC, so terminology might catch you out. The scope of the API therefore covers whatever you can use “Incoming Document” for: Purchase Invoice/Credit, Sales Invoice/Credit and Journal lines.
You need to use to HTTP calls to make this one work and this is the pattern:
Firstly you POST a simple line to say what you are attaching to i.e. our Invoice/Credit record or our journal line. This must be the ID or SystemID value of that record. How this plays out in a practical sense will depend on how you implement the API with your solution. In the scenario where you create a purchase invoice and then have an attachment of that latched against it you will need to perform the creation of the purchase invoice record first – a follow up post on this. Here is how the first action looks:
Body:
{ “parentId”: “5a086bc7-195f-eb11-89f9-0022481ab2d5”, “fileName”: “Example.pdf”}
Now the PATCH part is where the order of this becomes very important. You need a value from the POST to do the PATCH…..
Once the above is known then the PATCH command shown in Microsoft Docs becomes very easy to follow. Here is the command: PATCH businesscentralPrefix/companies({companyId})/attachments(parentId={parentId},id={attachmentId})/content . Simply add the information we now know. So in my case, with the above screenshot, my “parentID” is 5a086bc7-195f-eb11-89f9-0022481ab2d5 and my “id” is 7a1f7c6c-1e5f-eb11-89f9-0022481ab2d5. Notice that the odata.etag has been provided from the previous POST step so you can add that in as well for the “Headers” of the PATCH command. Here is what I mean:
In postman at least the body of the PATCH command is something like below but in a real solution you just need to make sure you pass a binary file:
Once each of the mentioned are in place you can go ahead with the PATCH. You should get a 204 status response but there will be no body type output.
This was originally posted here.
*This post is locked for comments