Archive

Posts Tagged ‘Azure Blob’

Canvas App | Display and Download large data sets of Images from Azure Blob Container with no delegation issues

In one of my previous articles, I detailed the steps to embed a Canvas app with upload image to Azure Blob feature.

In this article lets see, how to work with large data sets and achieve following:

  • Displaying blob images in Gallery.
  • Ability to Download the image.
Pre-requisites:
  • Azure Storage Account and Container. Get free Azure subscription here
  • Dynamics 365 account. Get 30 days trail here

Set up Azure Storage Account and Container

  • Connect to your Azure portal.
  • Create a new Storage Account and capture the Storage Account ‘Name’ and ‘Access Key’.
    • I’ve named Storage Account name as ‘expmar’
  • Create a new Container and copy the Name.
    • I named my Container as ’employee’.
  • Upload few images, which will be used to display in Canvas App.

Steps to create Canvas App and Connect to Azure Blob

Create Azure Blob Storage Connection:
  • Connect to Power Apps Portal using your Dynamics 365 account.
  • To connect to Azure Storage Account from Canvas App, we need a new ‘Azure Blob Storage’ Connection.
  • Click on ‘Data -> Connections -> Azure Blob Storage’ and provide Azure Storage Account ‘Name’ and ‘Access Key’ captured in previous section and click ‘Create’.
  • Up on successful creation, you should see the ‘Azure Blob Storage’ under ‘Connections’.
Steps to Create Canvas App and display blob images
  • Create a new Canvas App.
  • Add the ‘Azure Blob Storage’ connection created in previous step to the Canvas App from ‘Data -> Add a connection’.
  • As we established connection to Azure Storage Account, next, connect to ‘Container’ (i.e., Employee) and read the images to a List.
  • Use following formula on App’s ‘On Start’ event.
  • This formula, reads the images from ’employee’ Container and store the images in ’employeePhotos’ Collection.
  • Now we have the images in ’employeePhotos’ Collection, add a Gallery control and bind to ’employeePhotos’ Collection.
  • To display images, In the Gallery, add an ‘Image’ control and in the ‘Image’ property add following formula.
  • To display image name, add a ‘Label’ control and set Text as ‘ThisItem.DisplayName’.
  • To check whether images are rendering click on ‘App -> Run OnStart’ and you should see the images upload to Azure Blob Container in the Gallery.

Steps to Build Download feature
  • To download an image from Azure Container, we would need following 2 values:
    • URL : Azure Storage Account URL.
      • URL format would be “https://{Storage Account Name}.blob.core.windows.net
      • In my case, its “https://expmar.blob.core.windows.net”
    • SasToken (Shared access signature Token):
      • Generate the ‘Shared access signature’ as below from Azure portal.
  • Set ‘URL’ to ‘bloburl’ and ‘Token’ to ‘sastoken’ variables ‘OnStart’ of the App.
  • Complete ‘OnStart’ formula looks as below.
  • Add a ‘Download’ icon to the Gallery.
  • On ‘OnSelect’, use ‘Download(bloburl & ThisItem.Path & sastoken)’ formula.

Test the App
  • Run the App and you should see Images and ‘Download’ icon as below.
  • Click on ‘Download’ icon and image should get downloaded.

šŸ™‚

Advertisement
Categories: PowerApps Tags: , ,

[Step by Step] Embed “Azure Blob” connector Canvas App in Model Driven App

In this article, lets learn

  • Building a Canvas App with the functionality to upload and display images to/from Azure Blob.
  • Embed the Canvas App in Model Driven App.

Before we start, make sure you have the following prerequisites met.

Prerequisites:

  • PowerApps account. Refer here to procure an account.
  • Azure Storage Account and Container. Refer here for steps to create Azure Storage account.

Now that we know pre-requisites, lets understand the high level design:

  • Configure an Azure Container
  • Configure a ‘Model Driven’ App with OOB ‘Account’ entity.
  • ‘Embed’ Canvas app to ‘Account’ form.
  • Configure Canvas App with ‘Upload’ and ‘Display’ files to Azure Blob feature.
  • Save & Publish the Canvas App

Configure an Azure Container and capture ‘Access Keys’:

  • Add a new Container in Azure Storage Account.

Canv_8

  • Copy the ‘Storage account name’ and ‘Key1’ under ‘Access Keys’ tab of storage account, which would be needed in next steps.

Canv_9

Configure a ‘Model Driven’ App with ‘Account’ entity:

  • Configure a new ‘Model Driven’ App with OOB ‘Account’ entity.

Canv_22

‘Embed’ Canvas app to ‘Account’ form

When adding an embedded canvas app to a form always use a required field.Ā Refer article for detailed steps.

  • Open the ‘Account’ entity’s Main customization form.
  • Add a new ‘Tab’ to the form and place ‘Account Rating’ field.
  • Go to ‘Field Properties -> Controls’ tab of ‘Account Rating’ field.
  • Click on ‘Add Control’ and choose ‘Canvas app’ and click on ‘Add’

Canv_2

  • Click on ‘Customize’ which opens up a new ‘Canvas App’ with ‘ModelDrivenFormIntegration’ component and few fields.

Canv_3

Configure Canvas App with ‘Upload’ and ‘Display’ files to Azure Blob:

As we are building ‘Upload’ to Blob functionality we would’t need the default fields.

  • Delete the auto populated fields and ‘Form1’.
  • Add a ‘Add picture’ control to the form.

Canv_7

  • Add an ‘Azure Blob Storage’ connector and provide ‘Storage Account Name’ and ‘Account Access Key’ captured in previous steps.

Canv_10

Now that we established connectivity between our Canvas app and Azure Blob container. Lets add the Upload functionality.

  • Add ‘Upload’ button and ‘OnSelect’ add following statement to upload the file to blob
    • AzureBlobStorage.CreateFile(“canvasimages”,AddMediaButton1.FileName,AddMediaButton1.Media);
      • “canvasimages” is the Container Name.

Canv_11

  • Run the ‘Canvas App’ and browse and ‘Upload’ image.

Canv_12

  • Make sure the file gets saved to ‘Blob’ container.

Canv_13

Steps to display images in Gallery:

  • Add a Gallery and set the ‘items’ property as below
    • AzureBlobStorage.ListFolderV2(“JTJmY2FudmFzaW1hZ2Vz”).value
    • “JTJmY2FudmFzaW1hZ2Vz” is the Azure Container ID.

Canv_23

  • Now set the ‘Image’ property of image control as below
    • AzureBlobStorage.GetFileContent(ThisItem.Id)

Canv_16

Save & Publish the Canvas App:

  • Save the Canvas App.

Canv_17

  • Navigate to Model Driven app’s ‘Field Properties’ window.
  • You should see the ‘App Id’ populated.

Canv_18

  • Click ‘Ok’ and publish the customization’s.

Test the Embedded Canvas App:

  • Open the existing Account record.
  • Navigate to ‘Upload’ tab and you would get the Canvas App loaded.

Canv_21

  • Click on ‘Tap or click to add a picture’ to browse the image you would want to upload.
  • Click on ‘Upload’ button to upload image.
  • We can also add ‘Reset’ option using ‘Reset(AddMediaButton1)=true;‘ statement.
    • Also add “ModelDrivenFormIntegration.RefreshForm(“true”);” which refreshes the data on the host model-driven form.

Canv_24

Notes:

  • Refer guidelines while working with embedded canvas apps.

šŸ™‚

 

 

[Code Snippet] Upload file to Azure blob – C#

In this article I am going to provide details and code snippets on how to upload attachment to Azure blob storage from console application.

Prerequisites:Ā 

Below are the prerequisites to run the code snippet and upload the file

  • Azure subscription:You need an Azure subscription as the firstĀ  step.
    • You can spin up 30 days trail Azure subscription. Click here
    • Note: You need to share valid credit card details to complete the subscription and you will be charged 2 INR.
  • Storage Account:Add a storage account

Azure - Storage Account

  • Container:
    • Add a Container
    • Copy theĀ Container Name.

Azure - Storage Account - Containers

  • Access Keys:Need the ‘Key’ to connect to Azure Blob from your C# console.
    • Copy and keep below 2 values as shown in screenshot
      • Storage Account Name
      • Key 1

Azure - Storage Account - Keys

  • Nuget package:Add below nuget packages to your console project
      • Microsoft.WindowsAzure.Storage

C# Code Snippet:

// Namespaces

using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Auth;
using Microsoft.WindowsAzure.Storage.Blob;

private static void AddFileToBlob(){
var accountName = “{Storage Account Name}“; // Refer Prerequisites for value
var keyValue = “{key 1}“;Ā // Refer Prerequisites for value
var useHttps = true;
var connValid = true;

// Establish connection to Azure

var storageCredentials = new StorageCredentials(accountName, keyValue);
var storageAccount = new CloudStorageAccount(storageCredentials, useHttps);
var blobConString = storageAccount.ToString(connValid);

// Retrieve storage account from connection string.
storageAccount = CloudStorageAccount.Parse(blobConString);

// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

// Set container name
CloudBlobContainer container = blobClient.GetContainerReference(“{Container Name}“);Ā // Refer Prerequisites for value

// Set your blob name; It can be anything
CloudBlockBlob blockBlob = container.GetBlockBlobReference(“{Your desired blob name}“);

// Set your file path which you want to upload to blob
using (var fileStream = System.IO.File.OpenRead(@”D:\ABC.PNG”)) {
blockBlob.UploadFromStream(fileStream);
}

Console.WriteLine(“File added to Blob!!!”);
}

šŸ™‚

Categories: Azure Tags: ,

Move Attachments from CRM to Azure Blob – ā€œAttachment Managementā€ tool

December 17, 2017 3 comments

Recently I was exploring options to move attachments from CRM (i.e., Notes and Email) to Azure Blob storage and came across ā€œAttachment Managementā€ tool from Microsoft Labs.

This tool can be installed as a solution to your Dynamics 365 instance. Detailed steps and usage guide are available here.

In this article I am going to detail the imperative Azure and Dynamics 365 settings which would avoid the connectivity issues you may encounter during my usage.

Pre-requisites to use this solution:

  • Dynamics 365 instance (You can subscribe for 30 days trail)
  • Enable the ā€œAttachment Managementā€ solution from App Source.
  • Microsoft Azure Account (You can subscribe for trail)
    • Note: You can use the same Dynamics Office 365 account to register for Azure trail account.Ā 

Setting up Azure Storage Account:

Make sure your Azure Storage Account settings matches with below

  • Azure ā€œStorage Accountā€ ā€˜KIND’ must be ā€œBlobStorageā€.

Attachment - Azure Storage Account

  • ā€œStorage Accountā€ Containers ā€˜Public Access Level’ must be ā€œBlobā€.

Attachment - Containers

  • ā€œShared Access Signatureā€ (SAS token)
    • Set the ā€œStartā€ to Previous Day as the Azure time zone depends the data Center you choose. (For example, your IST date may be 1 day ahead of your ā€˜Data Center’ date which may make your ā€˜SAS token’ invalid)
    • Set the ā€œEndā€ to at least a week ahead of today.

Attachment - Shared Access Signature

Settings in Dynamics 365:

  • With the ā€œAttachment Managementā€ solution you will get 3 entities and a ā€˜Site Map Area’ (i.e., Azure Attachment Storage)
  • Make sure you configure settings only from ‘Azure Attachment Storage’Ā site map link and not from ā€˜Advanced Find’

Attachment - Sitemap

  • In ā€œAzure Blob Storage Setupā€ page, make sure the ā€˜Storage Account Name’, ā€˜Container names’ and ā€˜SAS token’ matches with Azure settings

Attachment - BlobStorageSettings

  • In ā€œNOTES ATTACHMENT SETTING ENTITYā€ page, pick your entities and provide correct ā€œContainerā€ name or leave that blank.
    • Note: If you have Dynamics Portal solution installed, uncheck the ā€˜Web Files’ entity, if its selected.

Attachment - NotesAttachment Page

Troubleshooting steps:

  • After you configure all the settings try adding a Note with attachment to the entity you configured.
  • If you don’t see your attachments move to Azure Container, go to ‘System Settings’ and look for “MicrosoftLabs.AttachmentManagement…..” job for exception.

Attachment - System Jobs

šŸ™‚