Advertisements

Archive

Posts Tagged ‘Azure’

Dynamics 365 – Using WebHooks to post data from Plugin to Azure Function

In my previous articles, I detailed the steps to create ‘Azure Functions’ and executing D365 SDK messages from ‘Azure Function’.

In this article, lets see how we fulfill Integration requirements using WebHooks model by submitting data to external WebAPIs and Services from D365.

What is a WebHook:

  • Webhooks is a lightweight HTTP pattern for connecting Web APIs and services with a publish/subscribe model.
  • Webhook senders notify receivers about events by making requests to receiver endpoints with some information about the events.

In this article, I am going to send data from Dynamics Plug-in to Azure Function using WebHooks model. So Plug-in acts as Webhook Sender and Azure Function acts as Receiver.

Below are the steps to create Azure Function and call Function from Plug-in by passing data.

Steps to create Azure Function:

  • Refer Create Azure Function article to create Azure Function Apps.
  • Create a new ‘Azure Function’ of type ‘Generic webhook’

wh1

  • Add below logic to Azure function which captures and logs the content posted from Plug-in

wh2

Get Azure Function URL:

Copy the ‘Azure Function’ URL along with key which will be used to communicate from Plugin.

  • Click on ‘Get function URL’ link and click ‘Copy’ to copy the URL

wh3

  • URL will have 3 parts
    • Endpoint URL
    • Code
    • ClientId
  • We would use only below 2 highlighted values while registering Plug-in.

wh4

Registering a WebHook:

  • Connect to Dynamics instance from Plug-in Registration tool
  • Click on ‘Register New Web Hook’

wh5

  • In the ‘WebHook Reistration’ page
    • Set ‘Endpoint URL’ as the ‘Endpoint URL’ value copied from ‘Azure Function URL’
    • Click ‘Add Property’
      • Set ‘Key’ as ‘x-functions-key’
      • Set ‘Values’ as ‘Code’ copied from ‘Azure Function URL’

wh6

Register Plug-in Step on WebHook

  • Register a Plug-in step on WebHook assembly

wh7

  • Create a step on ‘PostAccountCreation’

wh8

Test the WebHook:

  • Create an Account from D365
  • Check the Logs in Azure Function’s ‘Logs’ tab

🙂

 

Advertisements
Categories: Azure, CRM Tags: , ,

Code Snippet – Execute Dynamics 365 WhoAmIRequest in Azure Function

July 15, 2018 1 comment

Azure Function is a serverless compute service that enables you to run code on-demand without having to explicitly provision or manage infrastructure.

We can leverage ‘Azure Functions’ in Dynamics 365 to build robust integrations.

Scenario:

Lets take a scenario, where your Customer has a Facebook page and complaints posted on page should get created as ‘Case’ records in your Dynamics application.

In the above scenario,

  • Connecting to Facebook and retrieving Posts can be achieved using ‘Logic Apps’ Facebook connector
  • Now creating Posts as ‘Cases’ in Dynamics can be done by creating an ‘Azure Function’ with Case create logic and invoke it from ‘Logic App’

In this article, I will walk you through the steps to establish connection to D365 and  execute ‘WhoAmIRequest’ from ‘Azure Functions’.

Steps to create Azure Function:

  • Refer my previous article for steps to create Azure Function.

Prerequisites to Connect to D365 From Azure Function:

  • We would need ‘CRM SDK’ nuget packages in Azure Function to establish connection with D365.
  • Below are steps to add nuget packages to ‘Azure Function’
    • Connect to ‘Advanced tools(Kudu)‘ from ‘Function Apps -> Platform features
    • WhoAmi_1
    • Click on ‘Debug Console -> CMD’
    • WhoAmi_2
    • From the folder explorer, navigate to ‘site -> wwwroot‘ folder
    • Open the folder with your Azure Function name
      • Since my function name is ‘WhoAmI’ and I got the ‘WhoAmI’ folder under ‘wwwroot
    • WhoAmi_3
    • To refer nuget packages, we have to create a new file by name ‘project.json’
    • WhoAmi_4
    • Add below package references
    • WhoAmi_5
    • Save
  • Add URL and Credential details of ‘D365’ to ‘Application Settings’ of ‘Azure Function’
    • Navigate to ‘Function Apps -> Platform features -> Application Settings’
    • WhoAmi_6
    • Add the URL, UserId, Password details.
    • AzFunc_5

Code Snippet:

Once you have the Prerequisites ready, below is the code snippet to execute ‘WhoAmIRequest’

using System.Net;
using System.ServiceModel.Description;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Discovery;
using Microsoft.Crm.Sdk.Messages;
using System.Configuration;

public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
try{
log.Info(“Inside Try”);

ClientCredentials userCredentials = new ClientCredentials();
var userName = ConfigurationManager.AppSettings[“Crm_UserName”];
var password = ConfigurationManager.AppSettings[“Crm_Password”];
var Crm_UniqueOrgUrl = ConfigurationManager.AppSettings[“Crm_UniqueOrgUrl”];
userCredentials.UserName.UserName = userName;
userCredentials.UserName.Password = password;

log.Info(“userName – “+userName);
log.Info(“password – “+password);

var service = new OrganizationServiceProxy(new Uri(Crm_UniqueOrgUrl + “/XRMServices/2011/Organization.svc”), null, userCredentials, null);
service.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());

WhoAmIRequest reqWhoAmI = new WhoAmIRequest();
WhoAmIResponse resp = (WhoAmIResponse)service.Execute(reqWhoAmI);
var buID = resp.OrganizationId.ToString();
var userID = resp.UserId.ToString();

log.Info(“Business Unit Id – “+buID);}
catch(Exception ex)
{
log.Info(“Exception – “+ex.Message);
}

return req.CreateResponse(HttpStatusCode.OK, “Successfully made call to D365.”);
}

Run and Test the Code:

  • Click on ‘Run’ and expand ‘Logs’ section to track the logs.

WhoAmi_7

🙂

 

Azure – Create and Rename a Function

July 15, 2018 2 comments

Other day I was exploring Azure ‘Functions’ and had a tough time to rename the ‘Function’.

Below are the steps to create and rename Azure ‘Function’

What is an Azure Function:

Azure Function is a serverless compute service that enables you to run code on-demand without having to explicitly provision or manage infrastructure.

AzFunc_1

In simpler words, you can run your code with no Development/Hosting environment. All you need to do is start coding in ‘Azure Function’ editor like you code in Visual Studio, you can even refer external .dlls.

Steps to create Function:

  • Connect to your Azure Portal (http://portal.azure.com)
  • Create a new “Function App”
    • I named it as ‘AzrFunc’
  • Under the “Function App”, add a new ‘Function’ of type “Webhook + API”

AzFunc_3

  • Now a ‘Function’ will get created with a default name “HttpTriggerCSharp1”

AzFuncRename_1

Steps to rename a Function:

There is no rename option in UI to change the ‘Function’ name and you have to use ‘Console’ from “Platform features -> Development Tools”

  • Open the Console

AzFuncRename_3

  • Type command “ls” which lists out your function name
  • Use command “rename” to change the Function name
    • Syntax: rename <old_name> <new_name>
      • I renamed function to ‘WhoAmI’

AzFuncRename_4

  • Restart the ‘Function App’

AzFuncRename_6

  • Refresh your “Function App”, to see the change

AzFuncRename_5

Refer documentation to learn more about ‘Azure Functions’

🙂

Categories: Azure Tags: , ,

Dynamics 365 Data Archival and Retention Tool – MS Labs

February 12, 2018 2 comments

Data Archival and Retention is one of the common asks from customers.

Dynamics 365 product offering does not have any native data archival or retention feature, ‘Data Archival and Retention’ solution enables Dynamics 365 users to do archive of required data to COSMOS DB using Azure Services .

D 365 Data Archival

Download the solution from App Source and get started !

App Source : Link

User Guide : Link

🙂

Categories: Azure, CRM Tags: , ,

The reply address does not match the configured – Azure ADAL and Dynamics 365

December 27, 2017 Leave a comment

I was building a standalone HTML page which retrieves the Account records from Dynamics 365 and show them in a table.

As a prerequisite, I registered the application in Azure Active Directory’s ‘App registrations’.

OAuth Error - 1

Also configured the Application details in my HTML page

var organizationURI = “https://{orgname}.crm.dynamics.com”; //The URL to connect to CRM (online)
var tenant = “{Azure Tenant Name}.onmicrosoft.com”; //The name of the Azure AD organization you use
var clientId = “{Application ID}”; //The ApplicationId you got when you registered the application
var pageUrl = “http://localhost&#8221;; //The URL of your HTML page in your development environment.

After all the configuration, I was getting below error during the authentication

The reply address “http://&#8230;..” does not match the reply address configured for the application.

OAuth Error

Reason and Fix:

  • There must be a ‘Return URL’ configured with your application URL (i.e., HTML url in my case).
  • To configure the ‘Return URL’
    • Connect to ‘Azure Active Directory’ –> App registrations
    • Select your configured Application
    • Under ‘Settings’, click on ‘Reply URLs’
    • Add your application URL (i.e., HTML url in my case)

OAuth Error - 3

  • Rerun the Application and you should be able to connect and get Accounts

OAuth Error - 4

Refer this MSDN article for step by step to connect via ADAL

🙂

 

Categories: Azure, CRM Tags: , , ,

Azure Deployment Slots – Deploy and Swap

September 10, 2017 Leave a comment

Assume that you have your live application deployed on Azure running and you must deploy a hot fix.

The option you got is, you might want to take your application down for some time till your hot fix deployed.

How about you deploy the hot fix with no downtime, the answer is Azure “Deployment Slots”.

Create a Deployment Slot:

To use ‘Deployment Slots’, make sure you are running with Standard or Premium Azure Service Plan before adding a slot

  • Connect to Azure subscription and open your existing ‘App Service’
  • Under ‘DEPLOYMENT’ section, select ‘Deployment Slots’ and click on “Add Slot”

Azure - Deployment Slot

  • In the next screen, provide Slot name and choose ‘Configuration Source’. I gave my Slot name ‘Staging’ and in the ‘Configuration Source’:
    • If you choose your existing ‘App service’ all the settings including ‘Application Settings/Diagnostic Settings/…’ will be copied to this slot
    • Or you can choose ‘Don’t clone configuration….’ Option, if you want your slot to be clean and no carry over settings.

Deployment Slot - Add

  • Click ‘Ok’
  • After few seconds, you would get notification that your slot is ready
  • Once slot setup completed, your main App name will come as prefix of your slot (Ex: If ABC is my web app; my Slot name is ABC-staging)

Deployment Slot - Created

Publish Website to Slot:

  • Use Visual studio “Publish Web” option

Deploy - Connect Azure App Service

  • Select “Microsoft Azure App Service” and choose your main “App Service” and “Deployment Slots -> {Slot}”

Deploy - Slot

  • Click “Publish”
  • After publishing, your slot would work as any other Web App. You can give the slot URL to test to run BVT

Deployment Slot - Open Staging

Swap Slot:

  • Once you feel your slot is fine and can be promoted as main App
  • Click on ‘Swap’

Swap - 1

  • Select ‘Source’ and ‘Destination’
    • Main Web App would always represent as “production”
    • “Staging’ is the name of your slot

Swap - 2.PNG

  • Click Ok
  • It would take couple of minutes to complete Swap
  • Once Swap completes, end users who are accessing would start getting new build with virtually no down time.

Useful points:

  • If any errors are identified in production after a slot swap, roll the slots back to their pre-swap states by swapping the same two slots immediately
  • You can check your available ‘Deployment Slots’ by opening your Service Plans.

Check you available Slots.PNG

 

 

 

🙂

Azure Web Deploy – Time out errors

September 10, 2017 Leave a comment

Recently when we were trying to publish a web site to Azure ‘Deployment Slot’ using Visual Studio “Web Deploy” option, we encountered either Time out exception or VS hung after few files push.

Azure - Web Deploy - Visual Studio

Reason for this exception could be due to the enormous transactions on main (Prod) web app of the ‘Deployment Slot’.

Work around:

  • We found a work around by using Drag and Drop feature of Kudu service deploying to wwwroot folder directly.

Steps:

  • Publish the web site to your machines local drive

Publish Website - Local drive

  • ZIP the files (***You must select all the files and Zip it; Don’t zip the folder)

Publish Website - Zip folder

  • Open the Deployment Slot’s Kudu Service (Go to ‘Advanced Tools’ -> Go)

Web App - Advanced Tools

  • On the Kudu page, navigate to Home -> site -> wwwroot
  • Drag and drop the zipped folder of your published web site.

Kudu wwwroot - Drag and Drop

  • Wait for the process to complete publish.

🙂

Categories: ADX Tags: , , ,