Skip to main content

Notifications

Announcements

No record found.

Dynamics 365 Community / Forums / Finance forum / Save ER file to Azure ...
Finance forum
Unanswered

Save ER file to Azure File share instead of downloading it in local download folder

Posted on by 6,312
Hello,
I have created another post on this subject few days ago, but decided to create this one too for better explanations.
our goal is to save Customer payment xml file directly to the Azure File Share.
Currently it is downloaded automatically in the local download folder and then we manually upload the file into Azure, which takes much time.
We have seen the post below, but it may work only if the generate Payment doesn't use the ER.
 
In Azure File share we will have 2 separate folders. 
1 with currency EUR and another for any other currency.
Is there Possibility to achieve it with some setup, or writting a code will be 100% necessary?
We need it to be in one go, meaning, by clicking Oks on Generate Payment dialog box, file should be saved directly into Azure File Share.
 
One thing to mention also is that, currently, as mentioned the file is downloaded locally in download file and it is a .zip file, which contains xml file. We need this xml file, not the .zip file to be placed in Azure file share.
 
This is the class and method where it is determined if it should go to ER or not. In our case it will be always ER.
 
Thanks!
  • Johnny Profile Picture
    Johnny 6,312 on at
    Save ER file to Azure File share instead of downloading it in local download folder
    Update:
    This approach works perfectly, but it is to upload the file in Blob container.
    If I try to addapt it so file is uploaded to File share, then I get a "Bad Request" error.
     
    Here is my code for File Share which throws Bad request errors
     
    StorageCredentials  storageCredentials  = new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(credentialTable.AccountName, azKey); //Auth
    CloudStorageAccount storageAccount      = new Microsoft.WindowsAzure.Storage.CloudStorageAccount(storageCredentials, true);
    CloudFileClient     fileClient          = storageAccount.CreateCloudFileClient();
    CloudFileShare      root                = fileClient.GetShareReference('fileShareRoot'); //name of the file share
    
    if(!root.Exists(null, null))
    {
        return Checkfailed('Azure storage parameters are not set up correctly.');
    }
    
    CloudFileDirectory fileDirectory = root.GetRootDirectoryReference();
    
    fileDirectory.GetDirectoryReference('testFolder'); //folder in file share where the file should be uploaded
    
    CloudFile cloudFile = fileDirectory.GetFileReference(_args.getAttachmentName());
    
    if (_fileStream.CanSeek)
    {
        _fileStream.Seek(0, System.IO.SeekOrigin::Begin);
    }
    
    cloudFile.UploadFromStream(_fileStream, null, null, null); //here the Bad Request error is thrown
     
    Here is my Code for Blob container, which works perfectly:
     
    StorageCredentials  credentials     = new StorageCredentials(credentialTable.AccountName, azKey);
    CloudStorageAccount storageAccount  = new CloudStorageAccount(credentials, true);
    CloudBlobClient blobClient          = storageAccount.CreateCloudBlobClient();
    CloudBlobContainer rootContainer    = blobClient.GetContainerReference('testcon'); //Blob container name
    if(!rootContainer.Exists(null, null))
    {
        return Checkfailed('Azure storage parameters are not set up correctly.');
    }
    
    CloudBlobDirectory directory = rootContainer.GetDirectoryReference('testdir'); // folder name under container where file is uploaded
    CloudBlockBlob blockBlob =                directory.GetBlockBlobReference(strFmt(_args.getAttachmentName()));
    if (_fileStream.CanSeek)
    {
        _fileStream.Seek(0, System.IO.SeekOrigin::Begin);
    }
    blockBlob.UploadFromStream(_fileStream, null, null, null);
     
    I could not understand, what is going wrong in File Share case.
     
    Thanks!
  • Johnny Profile Picture
    Johnny 6,312 on at
    Save ER file to Azure File share instead of downloading it in local download folder
    Hi Arcadi,
    I'm still struggling with it : )))
    I also tried creating class library, but if I create it with .net standard 2.1 or .net 8.0, then I can't reference it with my x++ project.
    If I change it to net46 (which is same as x++ project), then the code does not work.
     
    But, I have found some examples of uploading ER file directly to file share, but it also does not work in my case.
     
     
    Here I do not get any error, but file is still not being uploaded to File share folder and instead it is still downloaded locally in my download folder.
    I have done setup of Document type and ER destination parameters and have set it to Azure Storage.
    Then I used and adapted the code above to a standard class below, which executes the ER files.
     
    [SubscribesTo(classStr(ERDocuManagementEvents), staticDelegateStr(ERDocuManagementEvents, attachingFile))]
    public static void ERDocuManagementEvents_attachingFile(ERDocuManagementAttachingFileEventArgs _args)
     
    First question would be under what framework version should I create my .net class library to reference it correctly to my x++ project?
    Second question would be, if you have ever used / extended "ERDocuManagementEvents_attachingFile"?
     
    Thanks
  • Arcadi  Profile Picture
    Arcadi 780 on at
    Save ER file to Azure File share instead of downloading it in local download folder
    Hi Johnny
     
    Yeah you could start by Microsoft example but the destination is different of course. In my case I read documentation of how to connect to an azure blob with C# and adapted it to X++. You also have the option to work directly with C#... For both options there is a lot of documentation.
     
    How our solution works, we hide the connection string in a key vault, we use the standard D365FO form of key vaults to connect to it and then on the destination we have a couple of fields, a lookup to select the record of the key vaults table and another for the blob name. But that's just one of the multiple ways you could implement your solution. 
     
     
  • Johnny Profile Picture
    Johnny 6,312 on at
    Save ER file to Azure File share instead of downloading it in local download folder
    Hello Arcadi,
    Thanks for the link.
    I have started creating the new destination, but it does not mention anything regarding the Azure, so I'm not really sure how to continue here.
    You mentioned you have done to for Azure Blob.
    Could you give more details? Did you just do everything that is described in the link you posted and then in the new "Path" field added the Azure Blob folder?
    How does it understand the credentials for Azure, etc?
  • Arcadi  Profile Picture
    Arcadi 780 on at
    Save ER file to Azure File share instead of downloading it in local download folder
    Hello Johnny, 
     
    About getting the XML instead of the ZIP is very easy, in the ER workspace you go to electronic reporting destinations, create a new one for your format with the ZIP node without destination and the XML with a destination, for example: 
     
     
    About sending them to Azure file share is more difficult, but not impossible. Off the top off my head I can think on two options:
     
    - Sending them to a Sharepoint and using Azure logic apps to move them to Azure file share. Just select as destination for the XML "Archive" and the File Type (Organization Administration / document Types) should be pointing to Sharepoint:
     
     
     
    Or you could using X++ create a new destination pointing to Azure file share (It's possible, I've created it for Azure blob storage): 
     
     
  • André Arnaud de Calavon Profile Picture
    André Arnaud de Cal... 287,773 Super User on at
    Save ER file to Azure File share instead of downloading it in local download folder
    Hi Johnny,

    Creating duplicate posts for the same question is not the correct way. Instead, you can provide additional information to your original question, 
    I think this is the other question. Save file directly to Azure storage account file share directory (dynamics.com)
    You updated it today. You could also provide the details you now wrote in this post. Having two posts on the same topic is not recommended. At least, now you are confusing me about the status and your attempts so far.

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