Skip to main content

Notifications

Finance | Project Operations, Human Resources, ...
Unanswered

How to fetch the file from Azure blob storage and save as an attachment using X++

Posted on by 5

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");)

  • Martin Dráb Profile Picture
    Martin Dráb 230,188 Most Valuable Professional on at
    RE: How to fetch the file from Azure blob storage and save as an attachment using X++

    Hi Mohammed, what is your question? If you want to share information rather than dicuss something, it's better to use a blog instead of a discussion forum.

    Also, your code is difficult to read, because it lacks indentation. Use Insert > Code (in the rich formatting view) to avoid this problem.

    And please ask questions about D365FO in a D365FO forum (such as Dynamics 365 Finance Forum).

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans