Monday, November 4, 2019

Upload Files To Azure BLOB From D365 FO


Uploading attachment file to Microsoft azure storage account blob.

  1.           Goto https://portal.azure.com
  2.             Goto Storage Account
  3. Click on Add 

  4. Fill details and click Review + Create
  5. Click on create. It will deploy the storage account.
  6. Go to newly created storage account go to access key .
    Key 1 and key 2 or connections strings will be used to access this storage account.
  7. Go to container and click Container
  8. Fill the name and click ok.
  9. Now we will create new runnable class in D365FO to upload file to newly created container.
    using Microsoft.WindowsAzure.Storage;
    using Microsoft.WindowsAzure.Storage.Blob;

    class UploadFileToAzureBlob
    {

        public static void main(Args _args)
        {
            DocuRef     docuref;
            /*const str connectionString =  "";*/
            const str containerName = "selflearningcontainer";
            str storageAccountName = "selflearningtest";
            str key = "”;

            System.IO.Stream attachmentStream = null;
            try
            {
    var storageCredentials = new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(storageAccountName, key);
    CloudStorageAccount storageAccount = new Microsoft.WindowsAzure.Storage.CloudStorageAccount(storageCredentials, true);
            
                if(storageAccount)
                {
                    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
     CloudBlobContainer blobContainer = blobClient.GetContainerReference(containerName);
                    blobContainer.CreateIfNotExistsAsync();

                    changecompany('USMF')
                    {
                        SalesId salesId = '001835';
                        SalesTable SalesTable;
                        //SalesTable salesTable = SalesTable::find(salesId);
                        select SalesId from salestable where SalesTable.salesid == '001835';
                
        select firstonly docuref where docuref.RefTableid == salesTable.TableId
                        && docuref.RefRecid == salesTable.RecId;
                    }

                    if(docuref.isValueAttached())
                    {
                        docuValue = docuref.docuValue();

    CloudBlockBlob blockblob =    blobContainer.GetBlockBlobReference("testingblob");
                        if(blockblob && !blockblob.Exists(null, null))
                        {
                            var storageProvider = docuvalue.getStorageProvider();
                            if(storageProvider != null)
                            {
     var docContents = storageProvider.GetFile(docuValue.createLocation());
                                attachmentStream = docContents.Content;

                                if(attachmentStream)
                                {
                                    blockblob.UploadFromStreamAsync(attachmentStream).Wait();

                                    blockBlob.FetchAttributes(null,null,null);
                                    BlobProperties BlobProperties = blockblob.Properties;
                                    if(BlobProperties.Length == attachmentStream.Length)
                                    {
                                        info("file uploaded successfully");
                                    }
                                }

                            }
                        }
                        else
                        {
                            info("file already exists");
                        }
                    }
                }
                else
                {
                    error("unable to connect azure file storage with the provided credentials");
                }
            }
            catch(Exception::Error)
            {
                error("Operation cannot be completed");
            }
            
        }

    }

  10. Now we will run the class 


D365FO. Dynamics 365 fiance and operation. Get and update contract of batch job on Controller run method. ax 2012

set Batch id in contract if batch isInBatch check is true in run method we will update the contract and set the batch id. class BrFunc...