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 :
Finance | Project Operations, Human Resources, ...
Suggested Answer

upload the file to Azure blob storage

(1) ShareShare
ReportReport
Posted on by 16
Hi team, 
 
Required assistance to complete the POC to upload the files to Azure blob storage using X++ code.
 
While i went through available team post on the same. few details seems to be missing like which all assembly we need to include. How we can include in?
 
 
Any available code sample (solution) that i can be tested as POC and later if required will modify the sample as per the requirement.
 
 
I have the same question (0)
  • Suggested answer
    Martin Dráb Profile Picture
    237,803 Most Valuable Professional on at
    I don't think you need to add any assemblies, because they (such as Azure.Storage.Blobs) are already there. The standard application already contains logic for working with blobs (e.g. BusinessEventsAzureBlobStorageAdapter class) and it utilizes the assembly.
  • Suggested answer
    Layan Jwei Profile Picture
    8,097 Super User 2025 Season 2 on at
    Hi Ashish,

    The link you provided doesn't work. However, here's a code that could help in how to upload to Azure blob by x++
    However, you need to make sure you define the stream first and replace "Storage account", "StorageKey", "BlobContainer"  and "fileName" with actual values
    
    
      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);
     

    Also, I think you'll find some examples when you do google search -- check if they can help
    https://samuelardilacarreno.com/2020/11/14/upload-files-to-an-azure-storage-x/
    https://axtechnicalblogger.blogspot.com/2019/11/upload-files-to-azure-blob-from-d365-fo.html

    Thanks,
    Layan Jweihan
  • Martin Dráb Profile Picture
    237,803 Most Valuable Professional on at
    You need to remove the extra slash from the end of the URL.
  • AshishAnand Profile Picture
    16 on at
    Hi 
    Thanks for the details. 
     
     
    ==> I was able to upload file to azure blob storage using the stand alone C# application. 
     
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using Azure.Storage.Blobs;
    using Microsoft.Extensions.Configuration;

    namespace AzureBlobStorage
    {
        class Program
        {
            static void Main(string[] args)
            {
                var config = GetConfiguration();

                var files = GetFiles(config["AzureStorage:SourceFolder"]);

                foreach(var file in files)
                {
                    Console.WriteLine(file.Name);
                }

                if (!files.Any())
                {
                    Console.WriteLine("Nothing to process");
                    return;
                }

                UploadFiles(files, config["AzureStorage:ConnectionString"], config["AzureStorage:Container"]);
            }
           
            //Write the code to read the configuration file from appsetting.json
            static IConfigurationRoot GetConfiguration()
               => new ConfigurationBuilder()
                   .SetBasePath(Directory.GetParent(AppContext.BaseDirectory).FullName)
                   .AddJsonFile("appsettings.json")
                   .Build();

            // function to get the files from the folder
            static IEnumerable<FileInfo> GetFiles(string sourceFolder)
              => new DirectoryInfo(sourceFolder)
                  .GetFiles()
                  .Where(f => !f.Attributes.HasFlag(FileAttributes.Hidden));


            //Write the code to upload file to azure blob
            static void UploadFiles(
               IEnumerable<FileInfo> files,
               string connectionString,
               string container)
            {
                var containerClient = new BlobContainerClient(connectionString, container);

                Console.WriteLine("Uploading files to blob storage");

                foreach (var file in files)
                {
                    try
                    {
                        var blobClient = containerClient.GetBlobClient(file.Name);
                        using (var fileStream = File.OpenRead(file.FullName))
                        {
                            blobClient.Upload(fileStream);
                        }

                        Console.WriteLine($"{file.Name} uploaded");

                        File.Delete(file.FullName);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex);
                    }
                }
            }

        }
    }

    Compile the code and run the code successfully.

    ===================================================>
    ​​​​​​​
  • AshishAnand Profile Picture
    16 on at
    Hi team, 
     
    While i was able to create the standalone C# console application to upload the file to azure blob storage. 
     
    Now i am not sure how i can replicate the same logic in X++
     
    - Should i create a X++ runnable class and just add (using Azure.Sto..) and use these methods directly 
     
    - should create a X++ class and add different methods to this. 
     
    - Should i add C# class library to the X++ solution and add reference of the C# code to X++ project. Write business logic by using C# and X++ source code - Finance & Operations | Dynamics 365 | Microsoft Learn
  • Martin Dráb Profile Picture
    237,803 Most Valuable Professional on at
    You suddenly start talking about a different assembly. If you want make things a bit easier for you, you may want to go back and use X++ code discussed below.
     
    If you want to use the other one, a good news is that even Azure.Storage.Blobs assembly is already include in F&O and you can use it directly.
     
    Both approaches are possible - you can either use .NET classes from directly from X++, or you can create a C# class library and use that from X++. The benefit of a C# library is that you can use features of C# that aren't available in X++, such as LINQ (that you're using in GetFiles()).
  • AshishAnand Profile Picture
    16 on at
    Thanks Martin.
     
    Instead of creating the console application say is created the C# class library using the same code.
     
    Will i able to just call the method from X++ just like i called these methods defined on main class of console application.?
     
    I will try to perform the same action and update if i got any blocker however once we say we can directly use the C# class in X++ can we directly use those method directly in X++
     
     
  • Martin Dráb Profile Picture
    237,803 Most Valuable Professional on at
    No, you shouldn't use the same code. Half of it wouldn't make sense in an F&O library: reading files from local disk (not applicable in cloud), using a configuration file, using Main(), writing to console.
     
    First of all, think about how you want to deal with files, because your current code is useless in cloud. Explain your business requirements if you have no idea how to design the application, so we can help you with the design.
  • AshishAnand Profile Picture
    16 on at
    My requirement is to upload the files to blob. (Authentication method to use is client id and client secret). 

    In D365FO also authentication should happen using the client id and client secret then finally the SSRS reports generated should be uploaded to blob.
     
    As part of requirement first i am trying to develope the stand alone C# console application and will be replicating the same in X++. 
  • Martin Dráb Profile Picture
    237,803 Most Valuable Professional on at
    Please tell us more about "the files"? Where are they located? If in your local machine, you'll need a mechanism to upload them to cloud. X++ running on web server in Azure can't simply connect to users' machine and copy files from their hard disks.

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…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

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

#1
Martin Dráb Profile Picture

Martin Dráb 664 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

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

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 303 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans