Showing posts with label AzureBlob. Show all posts
Showing posts with label AzureBlob. Show all posts

Sunday, October 13, 2019

How to specify a custom storage location for generated documents from Electronic reporting (ER) in D365FO


This blog guide you how to specify a custom storage location for generated documents from Electronic reporting (ER) in Dynamics 365 Finance and operations.

Generally when you generate payments, the payment file is generated, and you're asked to save it from your web browser to any available location.

Our client requirement is when they generate payments for vendors, the file should land automatically to private azure storage blob and from there our integration is configured such way that the files will be picked from blob and send it to Bank.

As we know that ER module classes locked almost and which can’t be reused or extend. But in out box Microsoft has provided API of the Electronic reporting (ER) Framework lets you extend the list of storage locations for documents that ER formats generates.

To implement the same we need to complete the below steps.¨

Ø  Setup electronic reporting destination settings
Ø  Subscribe to the AttachingFile() event in the class ERDocuManagementEvents and write business logic to save the file in desired location.


Setup electronic reporting destination settings:-

1. Document type will be setup to store the file to Azure storage.



2. Electronic reporting destination will be setup to store the file in the archive as shown below.



Then subscribe to the AttachingFile() event in the class ERDocuManagementEvents and write business logic to save the file in desired location:-

[SubscribesTo(classStr(ERDocuManagementEvents),staticDelegateStr(ERDocuManagementEvents,attachingFile))]
public static void ERDocuManagementEvents_attachingFile (ERDocuManagementAttachingFileEventArgs _args)
    {
        if (!_args.isHandled())
        {
            DocuType docuType = DocuType::find (_args.getDocuTypeId());
            _args.markAsHandled();
            var stream = _args.getStream();
            if (stream.CanSeek)
            {
                stream.Seek(0, System.IO.SeekOrigin::Begin);
            }
              //Here you can write your own desired locations where you want to save the file.
             // Else you can call C# helper class to save the file to azure blob which
            //I discussed in previous post
              JP_CloudStorageHelperClass.JP_CloudStorageHelperLocal helperClass =                                                             new JP_CloudStorageHelperClass.JP_CloudStorageHelperLocal helperClass();
               helperClass.saveFileInBlob(_StorageAccountName,Container, SASKey, BlobName , 
                                 System.IO.Stream stream)

        }
    }

For more info check Microsoft docs, Hope this helps and I will come up with another interesting blog post soon.

Happy Daxing :)

Tags
#D365, #ElectronicReportingDestinationSettings, #SaveCustomLocation, #AzureBlob

Saturday, October 12, 2019

Helper class to upload file to azure blob storage container with SASKey using C#

This blog walk thorough the helper class which help us to save file to desired private azure blob storage container using shared access signatures Key(SASKey).

using System;
using System.Net.Http;

namespace JP_CloudStorageHelperClass
{
    public class JP_CloudStorageHelperLocal
    {
        HttpClient httpClient = new HttpClient();

        //_StorageAccountName:- storage account name where you would like to save the file
        //_SASKey:- Which grant limited access to Azure Storage resources using SASKey
        //_BlobName: Name of the file 
        // _stream:- file stream you would like to drop to blob

        public Boolean saveFileInBlob(string _StorageAccountName, string _Container, string _SASKey = "", string _BlobName ="" , System.IO.Stream _stream = null)
        {
            string storageAddress = string.Format("https://{0}.blob.core.windows.net/", _StorageAccountName);

            //preparing URI to drop the file to blob
            Uri containerUrl = new Uri(storageAddress + $"{_Container}/{_BlobName}" + "?" + _SASKey);

            // which returns complete URI which is prepared in previous step
            string sasUri = containerUrl.AbsoluteUri;

            //x-ms-blob-type: <BlockBlob> - Returns the blob's type.
            httpClient.DefaultRequestHeaders.Add("x-ms-blob-type", "BlockBlob");

            // Drops the specified Uri as an asynchronous operation to blob
            HttpResponseMessage response = httpClient.PutAsync(sasUri,new StreamContent(_stream)).GetAwaiter().GetResult();

            if (response.IsSuccessStatusCode)
                return true;
            else
                return false;
        }
    }
}

This project can be referenced in D365FO project  and make use of this helper class for more details please check my previous post. of course there are direct class in X++ which can be used instead of this approach. this is just another way of doing it.

Hope this helps and I will come up with another interesting blog post soon.

Happy Daxing :)

Tags
#D365FO, #Azure, #AzureBlob, #Blob, #DropFileToBlob, #UploadFileToBlob