Wednesday, July 31, 2019

Write csv file on Azure Blob

##How to write csv file on Azure Blob from Dynamics 365 FO using X++?

This blog will explain how and why export data from dynamics 365 using X++ without data entity. There are some scenarios where we can not use directly the data entity to export data from dynamics 365 FO like incremental export. In this situation, we can use following way to export data in CSV/Excel format.
 
 public void WriteCSVFileOnBlob()
 {     
        System.Byte[]                       byteArray;
        System.Text.Encoding                encodingUTF8;
        str                                 accountName ="<<AccountNameOfBlob>>";
        str                                 accountKey ="<<AccountKeyofBlob>>";
str     blobName ="<<BlobName>>";
        str                                 fileName;
        str                                 strRowData;
        boolean                             isColumnsAdded=false;
        LogTable     logTable;
               
     

            if(logTable)
            {
                fileName="inputfilename.csv";

                Microsoft.WindowsAzure.Storage.Auth.StorageCredentials storageCredentials = new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(accountName, accountKey);

                Microsoft.WindowsAzure.Storage.CloudStorageAccount storageAccount = new Microsoft.WindowsAzure.Storage.CloudStorageAccount(storageCredentials, true);

                var blobcli = storageAccount.CreateCloudBlobClient();

                Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer cont = blobcli.GetContainerReference(blobName);

                CloudBlockBlob cloudBlockBlob = cont.GetBlockBlobReference(fileName);

                using (Microsoft.WindowsAzure.Storage.Blob.CloudBlobStream x = cloudBlockBlob.OpenWriteAsync().Result)
                {
                    while select logTable where logTable.InputfileName==fileName
                    {
                        if(logTable.RecId>0)
                        {
                            if(!isColumnsAdded)
                            {
                                isColumnsAdded=true;

                                strRowData= strFmt("%1,%2,%3,%4,%5,%6,%7,%8,%9","ItemCode","DocumentNumber","DocumentReference","WarehouseCode","ProcessName","DateTime","InputFileName","ErrorMessage","\r\n");

                                encodingUTF8 = System.Text.Encoding::get_UTF8();
                                byteArray = encodingUTF8.GetBytes(strRowData);
                                x.Write(byteArray,0,byteArray.Length);
                            }


                            strRowData=strFmt("%1,%2,%3,%4,%5,%6,%7,%8,%9",logTable.ItemCode,logTable.DocumentNumber,logTable.DocumentReference,logTable.WarehouseCode,enum2Str(ProcessType),logTable.Datetime,inputfilename,strReplace(logTable.Errormessage,",",";"),"\r\n");

                            encodingUTF8 = System.Text.Encoding::get_UTF8();
                            byteArray = encodingUTF8.GetBytes(strRowData);
                            x.Write(byteArray,0,byteArray.Length);

                        }

                    }

                    if(isColumnsAdded)
                    {
                        x.Flush();
                        x.Close();
                    }

                }
            }

    }