Dynamics 365 CE And Azure Function - Part Four

Introduction

This is my fourth article in the series of Dynamics 365 and Azure function integration. In earlier articles, we discussed the integration of Dynamics 365 with Azure, using hard-coded credentials and using the server to server authentication. Now, we are going to discuss how we can delete Azure blob after it is processed. You can refer to my earlier articles from the  following links.

Details

Once we have processed our blob storage (if you are new to blob storage, you can refer to  this KB), let’s say we want to delete it after our Azure Functions trigger has completed execution. We can delete it using the delete method from CloudBlockBlob class. To delete blob storage, we need the following three things -

Filename

This is the name of the blob storage file that we want to delete. We can get this parameter in our Run method in Azure Functions trigger, so we can pass the same.

ConnectionString

This is the connection string for the Azure blob storage. We can get it while navigating to integrate from our Azure Functions trigger like following.
 
Dynamics 365 CE and Azure Function
 
ContainerName

This is the name of the container which holds our blob storage. We can also get this by navigating to "Integrate from our Azure Functions" trigger. We need to use the string before the forward slash (/), like the following.
 
Dynamics 365 CE and Azure Function 
 
Now, we can use these details in the following code to delete the blob storage.
  1. public static void DeleteFromBlob(string filename, string connectionString, string containerName, TraceWriter log) {  
  2.  try {  
  3.   // Retrieve storage account from connection string.  
  4.   CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);  
  5.    
  6.   // Create the blob client.  
  7.   CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();  
  8.    
  9.   // Retrieve reference to a previously created container.  
  10.   CloudBlobContainer container = blobClient.GetContainerReference(containerName);  
  11.    
  12.   // Retrieve reference to a blob named "myblob.csv".  
  13.   CloudBlockBlob blockBlob = container.GetBlockBlobReference(filename);  
  14.    
  15.   // Delete the blob.  
  16.   blockBlob.Delete();  
  17.  } catch (Exception e) {  
  18.   log.Info($ "Error While delete blob storage {e.Message}");  
  19.  }  
  20. }  

Keep in mind that to use the above methods, we need to include the following reference in our Azure Functions trigger.

  1. #r "Microsoft.WindowsAzure.StorageClient"  
  2. using Microsoft.WindowsAzure;  
  3. using Microsoft.WindowsAzure.StorageClient;  

Stay tuned for more Dynamics 365 content!