Downloading document attachment file as BASE64 string in D365

Problem statement

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

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


Detailed resolution steps

  • Identify the record to which document is attached

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

  • System uses the standard document management class to get the attached document as container
  • BinData converts the container values into base64 string using bse64Encode 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));
        

/* H1 style */ h1 { font-size: 24px; /* Adjust the size as needed */ font-weight: normal; /* You can use 'bold', 'lighter', 'bolder', or a number */ } /* H2 style */ h2 { font-size: 20px; /* Adjust the size as needed */ font-weight: normal; /* Adjust the weight as needed */ } /* H3 style */ h3 { font-size: 18px; /* Adjust the size as needed */ font-weight: normal; /* Adjust the weight as needed */ }