Downloading document attachment file as BASE64 string in D365

How to Download Document Attachment as Base64 in D365 F&O

  • Retrieving the attached file from document management as a base64 string to pass it to the integration system.

In Dynamics 365 for Finance and Operations, files are stored as containers of data. We had a scenario to download this file and convert it into a base64 encoded string for transferring the data through the web service API.


Detailed resolution steps

  • Identify the record to which the document is attached

refrenceRecId – RecId of the table record to which the document is attached (Ex: CustGroup, CustTable recId’s)

  • The system uses the standard document management class to get the attached document as a container
  • BinData converts the container values into a base64 string using the base64Encode function.

Code snippet       


RefRecId        referenceRecId = 5637144576;

DocuRef         docuRef = DocuRef::find(curExt(), referenceRecId);

FileDataString  attachmentBase64String;

container       data = DocumentManagement::getAttachmentAsContainer(docuRef);

if (data && conLen(data) > 0)

{

   BinData binData = new BinData();

   binData.setData(data);

   attachmentBase64String = binData.base64Encode();

}

info (strFmt(“Base64 string %1”, attachmentBase64String));