web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :

Saving a file to Azure Blob container in D365FO

Chaitanya Golla Profile Picture Chaitanya Golla 17,225

Hi,

In this post we can view the X++ code that can be used to save file from D365FO to Azure blob container. For the demo purpose, I will consume the payment file generated during "Generate Payments" process. So, created an extension for the method closefile of class CustVendOutPaym.  

Please refer to my previous post to know the blob container details:

(+) Setting up a new Azure storage account and blob container to access from D365FO - Dynamics 365 Finance Community

Step 1: Create an extension for class CustVendOutPaym and using CoC wrap the closeFile method.

[ExtensionOf(classStr(CustVendOutPaym))]
final class CGCustVendOutPaym_Extension
{
public void closeFile()
{

}

}

Step 2: Provide definition for the method closeFile to capture the generated file and save to an Azure blob container. Used methods of class System.IO.File for this operation.

using System.Net;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.File;
using Microsoft.WindowsAzure.Storage.Blob;

[ExtensionOf(classStr(CustVendOutPaym))]
final class CGCustVendOutPaym_Extension
{
public void closeFile()
{
BlobProperties blobProperties;
StreamIo fileLocal = file;
StreamIo extnFile = file;
TextStreamIo textIo;
str storageAccountName = "daxologyfilestorage";
str key = "XXXX"; //Key value
str folderName = "Filedirectory";
#File

file = null;

System.IO.MemoryStream extnMemStream = extnFile.getStream();
var bytes = extnMemStream.ToArray();

try
{
using (System.IO.FileStream extnFileStrm = System.IO.File::Create(fileName, System.IO.FileMode::OpenOrCreate))
{
var storageCredentials = new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(storageAccountName, key);
CloudStorageAccount storageAccount = new Microsoft.WindowsAzure.Storage.CloudStorageAccount(storageCredentials, true);

extnFileStrm.Seek(0, System.IO.SeekOrigin::End);
extnFileStrm.Write(bytes, 0, bytes.Length);

if (storageAccount)
{
const str containerName = "daxologyContainer";

CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer blobContainer = blobClient.GetContainerReference(containerName);
blobContainer.CreateIfNotExistsAsync();

CloudBlockBlob blockBlob = blobContainer.GetBlockBlobReference(filename);

if (blockBlob && !blockBlob.Exists(null, null))
{
blockBlob.UploadFromStreamAsync(extnMemStream).Wait();

blockBlob.FetchAttributes(null, null, null);
blobProperties = blockBlob.Properties;
}

if (blobProperties.Length != extnMemStream.Length)
{
info("file not uploaded");
}

}
}
}
catch(Exception::Error)
{
error("Operation cannot be completed");
}

next closeFile();
}

}

Step 3: Build the code. Select the payment journal and click on "Generate payments" button to open Generate payments dialog. Click on Ok button on parameters dialog to view the output message and payment file in the desired location.

Payment File:

7658.PaymentFile.jpg

Output:

78018.Output.jpg

Thanks,

Chaitanya Golla

Comments

*This post is locked for comments