Often we find it difficult to export the documents / attachments from D365FO as there is no direct way to handle this ask.
I had similar requirement and managed to do it via Odata action and Logic apps. Logic apps need not be mandatory , but I prefer to follow the same integration pattern in my current assignment.
There are many out of box attachments entity ,in case you don`t have one for the requirement, follow the same approach as in one of the standard entities – CustomerAttachmentsV2 Entity, PurchaseOrderHeaderDocumentAttachmentEntity etc.,
DocumentID is a GUID which is auto generated and this serves as a unique key in most of the attachment entities. I am using this as a trump card and trying the following approach to retrieve the attachment.
DocuRef table holds the reference between the documents and their associated table and record.
Odata action is also a similar x++ method but it is decorated with the attribute ‘SysODataActionAttribute’. The method can be consumed by external system like an API call.
[SysODataActionAttribute('fetchAttachmentURLFromDocumentId',false)]
public static str fetchAttachmentURLFromDocumentId(DocuDocumentId _documentId, DataAreaId _company)
{
DocuRef docuRef;
str getURL ;
select firstonly docuRef
where docuRef.DocumentId == _documentId
&& docuRef.ActualCompanyId == _company;
if(docuRef)
{
getURL = File::SendFileToTempStore(DocumentManagement::getAttachmentStream(docuRef)
,ERDocuRef_Extension::filename(docuRef)
,classStr(FileUploadTemporaryStorageStrategy)
,true);
}
return getURL;
}
Above code snippet generates a downloadable URL which can be used to retrieve the attachment . Any format – file, outlook message, ppt, word , excel , pdf ..is supported. There is no external conversion needed for decoding or encoding , just download the file.
This is how I implemented in Logic apps.
Odata action is called in the POST method with the parameters as Legal Entity and DocumentId. With the above code, it retrieves the downloadable URL. It gives a downloadable link from the blob (if the DocuType is of Blob ).
The output can be then parsed to get the value of URL.
Above output from Downloadable URL can be then routed to any destination – Sharepoint , one Drive, SFTP and so on
After the above logic apps execution is completed, the file will be dropped in respective folder .
In my case, an outlook message file was uploaded and the same was saved in the sharepoint.
Original Post https://anithasantosh.wordpress.com/2024/08/20/export-and-download-attachment-d365fo-document-management/