Part-2: Azure BLOB storage with Dynamics 365 for operations

Consuming files stored in the Azure Blob Storage within Dynamics 365 Finance and Operations using X++ code 

Let’s consider a scenario where third-party business application uses Azure Blob as a central repository for storing the files, which in turn needs to be consumed by Dynamics 365 finance and operations. 

Following standard .Net libraries helps to consume those files:

  • using Microsoft.WindowsAzure.Storage;
  • using Microsoft.WindowsAzure.Storage.Blob;
  • using System.IO;

Part-2: Consuming files stored in the Azure Blob Storage within Dynamics 365 Finance and Operations using X++ code

Using the container connection created in Part-1 (Establish connection with the blob container) we can perform some basic operations with blob folder, as part of this blog we cover following area:

  1. Get file name list
  2. Get file memory stream
  3. Read the content of the file line by line
  4. Read the field value from the line

) Get File Name List

This section describes how to retrieve list of file name present in the blob folder using standard classes:

  • CloudBlobDirectory – Used to communicate with the blob folder
  • IEnumerable – Used here to store list of file names retrieved
  • IListBlobItem – Represents the list of blob items accessed
  • CloudBlockBlob –  Represents a blob that is uploaded as a set of blocks 

Using the connection established in previous section, following code will fetch the list of files names from the cloud blob directory:

  1. CloudBlobDirectory  cloudBlobDirectory;// The directory of the blob container
  2. container  con;
  3. cloudBlobDirectory = cloudBlobContainer.GetDirectoryReference(“SainaCloudConsulting\Test1”);//File path where the files are stored
  4. System.Collections.IEnumerable lstEnumarable = cloudBlobDirectory.ListBlobs(false, 0, null, null);
  5. System.Collections.IEnumerator lstEnumarator = lstEnumarable.GetEnumerator();
  6. List filenames = new List(Types::String); 
  7. while(lstEnumarator.MoveNext())
  8. {
  9.      IListBlobItem item = lstEnumarator.Current; 
  10.      if(item is CloudBlockBlob)
  11.      {
  12.           CloudBlockBlob blob = item;
  13.            blob.FetchAttributes(null, null, null);
  14.            con = str2con(blob.name, “/”);
  15.            filenames.addStart(conPeek(con,conlen(con)));
  16.      }
  17. }

2) Get file memory stream

After retrieving the file names list, iterate the file list to get each file names, and get the file memory stream content.

  1. CloudBlobDirectory  cloudBlobDirectory = _blobContainer.GetDirectoryReference(“SainaCloudConsulting\Test1”);
  2. CloudBlockBlob  blob = cloudBlobDirectory.GetBlockBlobReference(“TestFile.txt”);
  3. System.IO.Stream  memory = blob.OpenRead(null,null,null);

3) Read the content of the file line by line

Using the .Net standard IO class libraries, contents of the file can be read.

  1. System.IO.StreamReader streamReader = new System.IO.StreamReader(memory);
  2. Str   strRecord = streamReader.ReadLine(); //read each line in the file

4) Read the field value from the line

Assuming the data is separated using comma field delimiter, we store each field data into container variable as shown below:

  1. while(!System.String::IsNullOrEmpty(strRecord))
  2. { 
  3.      try
  4.      {
  5.           Container conRecord = str2con_RU(strRecord, ‘,’);// Here the file delimiter is ‘,’.
  6.           conPeek(conRecord, 1);// read first field data in the file line
  7.           conPeek(conRecord, 2);// read second field data
  8.      }
  9. }

This blog has covered the list of operations used to read the files from the blob container and perform necessary business action in Dynamics 365 Finance and Operation.