Skip to main content

Notifications

Announcements

No record found.

Saving a file to Azure file share from D365FO

Hi,

In this post we can view the X++ code that can be used to save file from D365FO to Azure file share. 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 file share details:

(+) Setting up a new Azure storage account and file share 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 file location. Used methods of class System.IO.File for this operation.

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

[ExtensionOf(classStr(CustVendOutPaym))]
final class CGCustVendOutPaym_Extension
{
public void closeFile()
{
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)
{
CloudFileClient fileClient = storageAccount.CreateCloudFileClient();
CloudFileShare fileShare = fileClient.GetShareReference("daxologyfileshare");

if (fileshare.Exists(null, null) == false)
{
throw Error("File share in the storage account not exists");
}

Microsoft.WindowsAzure.Storage.File.CloudFileDirectory cloudFileDir = fileShare.GetRootDirectoryReference();
container folders = str2con(folderName, '/');

int folder = 1;
while (folder <= conLen(folders))
{
str folderDirName = conPeek(folders, folder);

cloudFileDir = cloudFileDir.GetDirectoryReference(folderDirName);
cloudFileDir.CreateIfNotExists(null, null);

folder++;
}

Microsoft.WindowsAzure.Storage.File.CloudFile file = cloudFileDir.GetFileReference(filename);

if (extnFileStrm.CanSeek)
{
extnFileStrm.Seek(0, System.IO.SeekOrigin::Begin);
}

file.uploadFromStream(extnFileStrm, null, null, null);

}
}
}
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:

PaymentFile.jpg

Output:

File.jpg

Thanks,

Chaitanya Golla

Comments

*This post is locked for comments