Accessing Azure Blob storage from CRM Online Plugin

What is this about?

In this blog, we will see how to access Azure Blob storage and create document in CRM by reading a specific blob document in Azure.

Steps:

  1. Get the following details of the Azure Blob storage
    • Primary access key
    • Blob container name
    • File Name of the document
  2. Now we will write a sample plugin code which will read document from the Azure Blob storage and create an Annotation in CRM.

Since we cannot using external DLL in CRM online plugins, we are going to use the Web request to access the Azure blob storage. The HTTP web request has a bunch of headers along the above details to successfully access Azure blob storage.

For this, I have used a RestHelper and BlobHelper utility code files, which have all the operations of (a) making a web request and (b) performing blob operations.

The Helper files and the CRM plugin sample can be found in the below GitHub link:

GitHub: CRM Online Integration with Azure Blob

Using this we can get the document and create Annotation in CRM using the below code.

Please add your comments section and I would be happy to revert.

 #region Connect and fetch the data from Blob storage
 // Replace the below values with actual details from your Azure Blob storage
 string storageAccount = "blobstorageaccountname";
 string filename = "filenamehere"; // testdocument.pdf
 string containerName = "containernameHere"; //documents
 string storageKey = "primaryaccesskiyeofazureblobstorageaccount";


 BlobHelper blobHelper = new BlobHelper(storageAccount, storageKey);

 KeyValuePair<byte[], string> data = blobHelper.GetBlobResponse(containerName, filename);

 byte[] body = data.Key;
 string contentType = data.Value;
 #endregion

 #region Create Annotation in CRM
 string encodedData = System.Convert.ToBase64String(body);
 Entity Annotation = new Entity("annotation");
 Annotation.Attributes["objectid"] = new EntityReference(workOrder.LogicalName, workOrder.Id);
 Annotation.Attributes["objecttypecode"] = workOrder.LogicalName;
 Annotation.Attributes["subject"] = "Document from AX Integration";
 Annotation.Attributes["documentbody"] = encodedData;
 Annotation.Attributes["mimetype"] = contentType;
 Annotation.Attributes["notetext"] = "REST API - Sample document from AX.";
 Annotation.Attributes["filename"] = entity.GetAttributeValue<string>("cf_name");

 Guid annotation = service.Create(Annotation);
 #endregion
Advertisements

4 thoughts on “Accessing Azure Blob storage from CRM Online Plugin

  1. Thanks for uploading this. I’ve been checking out your blog for a while and it always brings me back!
    I’m a long time reader, but I’ve never been compelled to leave
    a comment until I started my own gaming blog.

    Like

  2. Hi Somesh,
    Thanks a lot for the custom plugin code.
    I have a small doubt though.
    While creating the plugin, the class BlobHelper shows “Missing Assembly Reference” for which you have used the reference using AzureBlobStorage.
    Now please tell me, how to include this AzureBlobStorage reference. As a class library or something else?

    Like

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s