Friday, August 12, 2022

Download an external file from Azure blob storage in D365 FO X++

Hi folks, 

This post will be useful in scenario where an external file is stored in Azure blob storage and users wants to download that external file without having it stored in D365 FO database. 

Requirement:

Recently, I came across a requirement where I had to access the external file from Azure blob storage and then download that external file for the user from D365 FO application although the file doesn't even exist in D365 FO database. 

Solution:

Below is the code snippet of the solution that can be used to download external file for the users.

 class ExternalFileDownload  
 {  
   public static void main(Args _args)  
   {  
     str url;  
     System.Net.HttpWebRequest httpRequest;  
     System.Net.HttpWebResponse httpResponse;  
     CLRObject clro;  
     System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();  
     Browser browser = new Browser();  
     url = "https://[azureblobstorageurloftenant].blob.core.windows.net/temporary-file/%7B81D4B98A-1989-4B13-B056-684FE6AA86C7%7D/CustGroupPackage_340228B4-1D45-4DC9-9931-5C4900457149_DMFPackage.zip?sv=2014-02-14&sr=b&sig=1uHDh1D%2FqDScyDXE8rPA7HfLgTCnleYd0cPp%2FCkmwwg%3D&st=2022-06-14T16%3A14%3A49Z&se=2022-06-14T17%3A19%3A49Z&sp=r";  
     clro = System.Net.WebRequest::Create(url);  
     httpRequest = clro;  
     httpRequest.set_Method("GET");  
     httpRequest.set_KeepAlive(true);  
     httpRequest.set_ContentType("application/json");  
     httpResponse = httpRequest.GetResponse();  
     httpResponse.GetResponseStream().CopyTo(memoryStream);  
     FileUploadTemporaryStorageResult result = File::SendFileToTempStore_GetResult(memoryStream, "Sample");  
     browser.navigate(result.getDownloadUrl(), true);  
     memoryStream.Close();  
     httpResponse.Close();  
   }  
 }  

No comments:

Post a Comment

Enable and Disable Vendor invoice button on PO list and details page X++

Hi folks, This blog will be useful where vendor invoice process is required to be controlled based on certain conditions. Requirements: Proc...