X++ code in D365 F&O for fetching the file from azure blob storage and attaching it to respective table like "PurchTable" as an attachment .
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
class FetchAndUploadAttachment
{
public static void attachFileFromBlobStorage(Common _common, Filename _fileName, str _notes = '')
{
Microsoft.Dynamics.AX.Framework.FileManagement.IDocumentStorageProvider storageProvider;
PersonnelIntegrationStorageAccountConnectionString azureStorageKey = personnelIntegrationConfiguration::find().editEncryptDecryptStorageAccountConnectionString(false);
Microsoft.WindowsAzure.Storage.CloudStorageAccount storageAccount = Microsoft.WindowsAzure.Storage.CloudStorageAccount::Parse(azureStorageKey);
var blobcli = storageAccount.CreateCloudBlobClient();
Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer cont = blobcli.GetContainerReference("File path of azure blob storage folder");
CloudBlockBlob cloudBlockBlob = cont.GetBlockBlobReference(_fileName);
System.IO.Stream memory = cloudBlockBlob.OpenRead(null,null,null);
Filename attachmentName = System.IO.Path::GetFileNameWithoutExtension(_fileName);
str fileContentType = System.Web.MimeMapping::GetMimeMapping(_fileName);
DocuRef docuRef;
docuRef = DocumentManagement::attachFileToCommon(_common, DocuType::typeFile(), memory, _fileName, fileContentType, attachmentName, _notes);
if (docuRef.RecId)
{
info("Attached the file to the respective table record");
}
}
}
Note : Call this mehod from whereever you want based on your requirements(FetchAndUploadAttachment::attachFileFromBlobStorage(purchtable, "FileName.pdf", "this is pdf file");)