Skip to main content

Notifications

Announcements

No record found.

Dynamics 365 Community / Forums / Finance forum / Create file from D365 ...
Finance forum
Suggested answer

Create file from D365 FO to Azure Files (not blob or container) share

Posted on by 451

Hi ,

I want to develop functionality to save a file to Azure FILE SHARES  and not container or blob files.

I have seen lots of code to upload file to Azure blob storage and viceversa however I want to check if there is something related to uploading files in Azure files share from D365 FO .

There are some codes but in diff formats eg. just loop the folders name etc. but not creation,'

We are using file share because we need to map this folder to network drive and use it further.

EG . storage.createcloudfileclient().

pastedimage1652088287758v1.png

Also I would like to know if it is possible to mount a Container to a network drive and what's the main difference between file shared/ container and blob storage as it is quite similar in saving files.

Thanks,

VK

  • Martin Dráb Profile Picture
    Martin Dráb 225,513 Super User on at
    RE: Create file from D365 FO to Azure Files (not blob or container) share

    Then write text directly to a stream, which you can then easily pass Upload(). You can either use *StreamIO classes in X++, or use .NET stream classes directly.

  • vicky1234 Profile Picture
    vicky1234 451 on at
    RE: Create file from D365 FO to Azure Files (not blob or container) share

    The requirement is that a file will be created on the fly and it should be uploaded in the File share directory .

    So in this case a batch job running at eod will create a txt file via code lets say vendor details and then this file will needs to be saved to the file share.

    Creating file via code is ok for me . the thing which I want to check is that how do I pass/create  a file/stream via the code related to CloudFileClient which will upload file to Azure files ?

    Thanks,

    VK

  • Martin Dráb Profile Picture
    Martin Dráb 225,513 Super User on at
    RE: Create file from D365 FO to Azure Files (not blob or container) share

    Can you please elaborate where do you want to take the data from? What exactly do you mean by "I created a text file"? Do you need to stare a file somewhere in F&O, such as a in a document attachement? If not, can't you write your text directly to a memory stream?

    I would help me a lot, and increase your chance to get an answer, if you provided more information by yourself, without waiting for my interrogation.

  • vicky1234 Profile Picture
    vicky1234 451 on at
    RE: Create file from D365 FO to Azure Files (not blob or container) share

    Hi Martin,

    I am using the below code and I would like to create files from AX on click of some button.

    So lets say I created a text file and that needs to be stored in the location.

    System.IO.MemoryStream memoryStream;
     
            var storageCredentials = new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials('filedemostorage', 'abc/==');
     
            CloudStorageAccount storageAccount = new Microsoft.WindowsAzure.Storage.CloudStorageAccount(storageCredentials, true);
     
            CloudFileClient fileClient = storageAccount.CreateCloudFileClient();
            
            CloudFileShare share = fileClient.GetShareReference('filesharedemo');
     
            if (share.Exists(null, null))
            {
                CloudFileDirectory rootDir = share.GetRootDirectoryReference();
     
                CloudFileDirectory fileDir = rootDir.GetDirectoryReference('filedirectory');
     
                if (fileDir.Exists(null, null))
                {
                    CloudFile file = fileDir.GetFileReference('notepaddoc');
                   //// if (file.Exists(null, null))
                   // {
                   //     memoryStream = new System.IO.MemoryStream();
                   //     file.DownloadToStream(memoryStream, null, null, null);
                   // }
                }
                  
            }
    

    May be i need to use memory stream to convert , but I am not sure if some one hastried this before as I dont see this code for D365 FO x and hence need to check .
    Once done i will reset the storage parameters etc.
    So now i need to make a workable job to upload to a file storage which now is not there.

  • Martin Dráb Profile Picture
    Martin Dráb 225,513 Super User on at
    RE: Create file from D365 FO to Azure Files (not blob or container) share

    Are you trying to write X++ or C#? You said you wanted X++, but you have C# code instead.

    If you meant X++, your code is completely wrong. For example, there is no namespace keyword in X++, the type name for strings is str instead of string and so on. Forget ConfigurationManager completely and used a hard-coded value for myconnectionString for now. When you get your code working, you can create a table field to store the value. Also, localFilePath makes little sense, since X++ will run in cloud and not on a local machine.

  • vicky1234 Profile Picture
    vicky1234 451 on at
    RE: Create file from D365 FO to Azure Files (not blob or container) share

    Thanks Martin,

    I tried a couple of times to create the text file and upload via the ShareFileClient, but it seems that the namespace is not correct.

    Can you kindly help me with a sample job in X to upload with NameSpaces to be used..

    I tried the below job with some modifications.

    using Azure;
    using Azure.Storage.Files.Shares;
    using System.Configuration;
    using System.IO;
    
    namespace PrjCreateShareToUploadFile
    {
        class Program
        {
            static void Main(string[] args)
            {
                string myconnectionString = ConfigurationManager.ConnectionStrings["StorageConnection"].ConnectionString.ToString();
                string myshareName = "Mysampleshare";
                string mydirName = "Mysampledir";
                string myfileName = "Mysamplefile";
                string localFilePath = @ConfigurationManager.ConnectionStrings["FilePath"].ConnectionString.ToString();
                // Get a reference to a share and then create it
                ShareClient myshare = new ShareClient(myconnectionString, myshareName);
                myshare.Create();
                ShareDirectoryClient directory = myshare.GetDirectoryClient(mydirName);
                directory.Create();
                ShareFileClient myfile = directory.GetFileClient(myfileName);
                FileStream stream = File.OpenRead(localFilePath);
                myfile.Create(stream.Length);
                myfile.UploadRange( new HttpRange(0, stream.Length), stream);
            }
        }
    }

    Thanks,

  • Suggested answer
    Martin Dráb Profile Picture
    Martin Dráb 225,513 Super User on at
    RE: Create file from D365 FO to Azure Files (not blob or container) share

    A good starting point is Azure documentation, namely Azure Storage samples. There you can find a link called Create a share and upload a file, which will give you sample code in C#,

    You can either use C# code directly in a class library (that you'll call from X++), or you can use Azure storage classes directly from X++. The sample code tells you that the key class is ShareFileClient and you can again look into documentation for details. If you want to call it directly from X++, use synchronous variants of the methods, such as Create instead of CreateAsync.

Helpful resources

Quick Links

Replay now available! Dynamics 365 Community Call (CRM Edition)

Catch up on the first D365 Community Call held on 7/10

Community Spotlight of the Month

Kudos to Saurav Dhyani!

Congratulations to the June Top 10 community leaders!

These stars go above and beyond . . .

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 287,773 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 225,513 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans