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

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

How to send container records as CSV to azure blob

(0) ShareShare
ReportReport
Posted on by 1,907

Hi i have a container that has multiple records (lines)

I want to be able to send it to azure blob as csv

I'm assuming i need to convert the container to sth that can be accepted by azure blob

So what should i do exactly? 

I have the same question (0)
  • Martin Dráb Profile Picture
    236,503 Most Valuable Professional on at
    RE: How to send container records as CSV to azure blob

    Do you mean attaching it to a table record D365FO when document management is configured to store attachments in Azure blob storage, or sending it to your own blob storage (independent on D365FO)?

  • .. Profile Picture
    1,907 on at
    RE: How to send container records as CSV to azure blob

    Hi Martin,

    My own blob storage. (however i would like to know the difference between the two options now that you've mentioned it) 

  • Verified answer
    Martin Dráb Profile Picture
    236,503 Most Valuable Professional on at
    RE: How to send container records as CSV to azure blob

    What do you have in the container? Do I understand correctly that it's a list of strings? If so, you need to create CSV from this container before you can store CSV to a file. CommaStreamIo looks like the right class for this purpose.

    Azure Storage offers a .NET API that you can call via .NET Interop from X++. You need CloudBlobClient class, in particular. With this knowledge, you can find examples by searching for D365FO and CloudBlobClient in your favourite search engine.

    If you worked with document management, you would simply use Docu* classes in AX (e.g. DocumentManagement::attachFile()).

  • .. Profile Picture
    1,907 on at
    RE: How to send container records as CSV to azure blob

    Hi Martin,

    How can i convert the container to CSV?

    I've seen an example where they do a while loop on a record and use io.writeExp

    However is there a way to convert this contsiner directly to CSV? Using streamIO

  • Martin Dráb Profile Picture
    236,503 Most Valuable Professional on at
    RE: How to send container records as CSV to azure blob

    Yes, it is possible. You seemed to overlook my suggestion: CommaStreamIo class.

  • .. Profile Picture
    1,907 on at
    RE: How to send container records as CSV to azure blob

    Hi martin,

    No i didn't overlook it. By saying streamIo.. I mean commaStreamIo.

    So when u mentioned it i read about it.

    My question was should i loop through the container? Or is there a way i can pass the container and it will convert it to CSV?

  • .. Profile Picture
    1,907 on at
    RE: How to send container records as CSV to azure blob

    Hi Martin,

    In ax12, the old service used commaTextIo

    By using the method Write and passing container to it.

    What should i do to see the file after using write method?

    Also what's the difference between using commaTextIo and CommaStreamIo? 

  • Martin Dráb Profile Picture
    236,503 Most Valuable Professional on at
    RE: How to send container records as CSV to azure blob

    Please remember that I still don't know what you have in the container, therefore I can't say much about how you should process it. I can only assume that you want to do something with each element, therefore you'll indeed need to iterate the container to get elements one by one. But if you just wanted to create lines (because you already have lines content ready), you could do that with con2str(). Or maybe you have something else is in the container and you need to do something completely different.

    Why do you want to "see the file"? I thought you want to write it to a blog, not to display it.

    CommaStreamIO writes data to a stream, which you can pass to CloudBlockBlob.UploadFromStream(), for example. CommaTextIo writes to a file on disk.

  • .. Profile Picture
    1,907 on at
    RE: How to send container records as CSV to azure blob

    Hi Matrin,

    Thanks alot. yeah my container has string values.

    What i did is, while filling each  container i used commaStreamIo write method and i was able to receive the file successfully to azure blob ( it was a quick test).

    Here's what i did:

    contianer lines;
        CommaStreamIo comma = CommaStreamIo::constructForWrite();
        comma.outFieldDelimiter(",");
        str fileName = "Test.csv"
        comma.writeExp(lines); // assume lines is filled
    
            System.IO.Stream        stream          = comma.getStream();
    
            Microsoft.WindowsAzure.Storage.Auth.StorageCredentials  storageCredentials  = new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(StorageAccountName, StorageKey);
            Microsoft.WindowsAzure.Storage.CloudStorageAccount      storageAccount      = new Microsoft.WindowsAzure.Storage.CloudStorageAccount(storageCredentials, true);
            var                                                     blobcli             = storageAccount.CreateCloudBlobClient();
            Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer  cont                = blobcli.GetContainerReference(BlobContainer);
    
            stream.Position=0;
    
            Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob      cloudBlockBlob      = cont.GetBlockBlobReference(fileName);
            cloudBlockBlob.UploadFromStream(stream,null,null,null);
     

  • Verified answer
    Martin Dráb Profile Picture
    236,503 Most Valuable Professional on at
    RE: How to send container records as CSV to azure blob

    Good! It seems that the problem is resolved. Please don't forget verify answer(s).

    By the way, you can greatly simplify your code by utilizing the 'using' statement. Like this:

    using System.IO;
    using Microsoft.WindowsAzure.Storage;
    using Microsoft.WindowsAzure.Storage.Auth;
    using Microsoft.WindowsAzure.Storage.Blob;
    
    class MyClass
    {
        void export()
        {
            container lines;
            CommaStreamIo comma = CommaStreamIo::constructForWrite();
            comma.outFieldDelimiter(',');
            str fileName = 'Test.csv';
            comma.writeExp(lines); // assume lines is filled
    
            Stream stream = comma.getStream();
            stream.Position = 0;
    
            StorageCredentials storageCredentials = new StorageCredentials(StorageAccountName, StorageKey);
            CloudStorageAccount storageAccount = new CloudStorageAccount(storageCredentials, true);
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
            CloudBlobContainer cont = blobClient.GetContainerReference(BlobContainer);
    
            CloudBlockBlob cloudBlockBlob = cont.GetBlockBlobReference(fileName);
            cloudBlockBlob.UploadFromStream(stream, null, null, null);
        }
    }

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
CA Neeraj Kumar Profile Picture

CA Neeraj Kumar 1,882

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 794 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 525 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans