Let’s get started with Azure Function for Dynamics 365 CRM: Part 1 [Updated Connection Code]

Amit Prajapati
4 min readFeb 20, 2021

--

In this blog, we will learn how to create an Azure Function App to connect it with Dynamics 365 CRM and perform the CRUD operation in Dynamics 365 CRM.

First, we will create an Azure Function project for Dynamics 365 CRM from Visual Studio, and below are the steps for the same.

Step 1: Create Azure Function Project in Visual Studio

Open Visual Studio and create a new Azure Function Project. Filter platform as Azure, select Azure Function, and click on Next.

Create Azure Function Project in Visual Studio

Name your project and click on Create button.

Name the Azure Function Project

After clicking on Create, you will get the below screen where you need to select the Trigger and Azure Function v1 (.Net Framework) and click on Create.

In my case, the Azure Function trigger will be Http Trigger which means when an azure function will receive an HTTP request it will trigger.

Select HTTP Trigger for the Azure Function App as triggering point

Your Azure Project will be created and the following will be the structure of the project.

Project Structure for Azure Function

Step 2: Add NuGet Package and Update Azure Function Code to Connect with Dynamics 365 CRM

Now, we will create Azure Function to connect with the Dynamics 365 CRM we need to add the required NuGet Package [Microsoft.CrmSdk.CoreAssemblies]. To add the NuGet Package right-click on the Project and click on Manage Nuget packages.

Click on the Manage NuGet Packages..

Add Microsoft.CrmSdk.CoreAssemblies in your Project.

Select the Microsoft SDK for CRM with 9.0.2.5

Following is the code to connect the Dynamics 365 CRM. Currently, we are going to use the credential by specifying them in the C# Code or you can use it as constant. [Updated Connection Code]

Note: Follow this blog to Client ID and ClientSecret for your Dynamics 365 CRM (You need to create the application user )

Later in the series, we will learn how to use Environment Variable and pass the credential more secure way in Azure Function using Azure Key Vault. [Stay tuned..!!]

Now, select the code and Refactor the code and make the connection function that will return the IOrganizationService if the connection is established else return null.

Refactoring the Service Connection Code

Now, the final code will be as below:

Step 3: Create Customer in Dynamics 365 CRM when Azure Function is triggered.

Now, we will create a customer with a Hardcoded name when the function is triggered with an HTTP request. Update the existing code with the below code:

Connection Code after Refactoring the Code.

Step 4: Azure Function Testing

We will require the API testing tool, here I am using Postman and the following is the link to download “Postman”. https://www.postman.com/downloads/

To test the application, click on the Start button on top of Navbar as mentioned below in the screenshot [Button will have Project Name]. It will take a few minutes to Load the Azure Emulator

Run the Function App on the Local machine for testing.

Following is the screen you will be able to see and copy the URL highlighted in the red below and paste that URL in Postman.

Azure Function Tool

Open the Postman and click on the create a new tab

Postman Dashboard

Select request as POST and paste the URL:

After pasting the URL, click on Send

You will get the following response on the Azure Function Tool and Postman

The result after Sending Post Request to Azure Function

If there any error or issue with the Azure Function code, the request will be failed and will be displayed on both Azure Function Tool and Postman [Status will be “4**” or “5**” ]

Now, we will take look at Dynamics 365 CRM environment and check whether the account is created or not.

Result

Created customer from Azure Function.

We are just getting started with Azure Function for Dynamics 365 CRM and stay tuned for more in this series.

Upcoming blogs

1. How to use Dynamics 365 Credentials securely using Azures Function.

2. How to create Dynamics 365 integration with Third-party Applications.

Many more……

--

--

Amit Prajapati
Amit Prajapati

Written by Amit Prajapati

I am Technical Consultant from Mumbai, India. I like to contribute my knowledge to the community and good social cause.