Azure Development, dynamics 365, Uncategorized

Download file from Azure BLOB storage inside Dynamics 365 Plugin or custom WF activity

Problem: Microsoft Labs AttachmentManagement solution doesn’t allow to download attachments via plugin for files,synced to Azure BLOB storage .

Solution: download BLOB, using a custom plugin or custom WF activity.

Mind mapping(please don’t try any of this, it doesn’t work):

first, I found this link:

https://community.dynamics.com/365/b/heralddyncrm/archive/2018/05/27/upload-and-download-files-in-azure-blob-using-c

It worked perfectly while I was unit testing.

Then I wrapped it into the WF activity. Registered dll. Booom! It didn’t work! Obviously. That solution required external dlls which can’t be referenced, because… because of the Sandbox, online etc.

I tried this:

https://nishantrana.me/2017/05/17/using-ilmerge-for-plugin-in-crm/

It’s cool, very cool, but unsupported and … it didn’t work either. I was getting something like this: System.TypeLoadException: Inheritance security rules violated while overriding member:…”

Then I found RestHelper and BlobHelper. It was close, but not close enough:

https://community.dynamics.com/crm/b/dynamicscrmbestpractices/archive/2016/11/01/accessing-azure-blob-storage-from-crm-online-plugin

https://github.com/somesh2207/Azure-Blob-operations

Nothing was working for me! At all. But CRM God loves me, so – HOORAY! That was it:

http://www.denbraver.com/wordpress/2018/06/04/download-from-azure-blob-using-the-azure-rest-api/

The Conclusion:

Bad news: to fetch Azure BLOB inside Dynamics 365 plugin or custom WF activity you can’t use any of this, because you will not be able to reference any of those fancy dlls:

string storageConnection = CloudConfigurationManager.GetSetting("BlobStorageConnectionString");  CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(storageConnection); 
CloudBlobClient blobClient = cloudStorageAccount.CreateCloudBlobClient();
CloudBlobContainer cloudBlobContainer = blobClient.GetContainerReference(containerName); 
CloudBlockBlob blockBlob = cloudBlobContainer.GetBlockBlobReference("filename.ext");
MemoryStream memStream = new MemoryStream();
blockBlob.DownloadToStream(memStream);

Good newsyou don’t need to, if you use SAS token, no “crazy” libraries or headers either.It just works:

string uri = String.Format("https://{0}.blob.core.windows.net/{1}/{2}{3}", storageAccount, containerName, filename, SAS);
HttpWebRequest request = HttpWebRequest.Create(uri) as HttpWebRequest; 
request.Method = "GET";
request.ContentLength = 0;
MemoryStream memoryStream = new MemoryStream(0x10000);
using (Stream responseStream = request.GetResponse().GetResponseStream()){
    byte[] buffer = new byte[0x1000];
    int bytes;
    while ((bytes = responseStream.Read(buffer, 0, buffer.Length)) > 0){
          memoryStream.Write(buffer, 0, bytes);
   }
}   
byte[] response = memoryStream.ToArray();

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s