Skip to content
Cover image for the article Easy steps to develop and deploy WebJobs (Azure SQL to D365)

Easy steps to develop and deploy WebJobs (Azure SQL to D365)

A step-by-step guide to building a console app that pulls data from Azure SQL into Dynamics 365 CRM and deploying it as a scheduled Azure WebJob.

Automation & IntegrationSamuel Mothiba4 min read

In today's business landscape, data integration has become increasingly essential to achieving a competitive edge. The ability to access data from disparate systems such as Dynamics 365 and Azure SQL is necessary to make informed decisions. In this article, we will discuss how to write a console application that gets data from Azure SQL and saves it into Dynamics CRM. We will also explore how to deploy the console app as a WebJob to run after every two hours. The data that we will be collecting is order records and related order items.

Step 1: Create the console application

The first step is to create a new console application project in Visual Studio. To do this, open Visual Studio and navigate to File > New > Project. Select Console Application from the list of available templates and give your project a name.

Step 2: Add the necessary references

To access Dynamics 365 and Azure SQL, we need to add the required references. Right-click on your project in the Solution Explorer and select Manage NuGet Packages. In the NuGet Package Manager window, search for and install the following packages:

  • Microsoft.CrmSdk.CoreAssemblies
  • Microsoft.CrmSdk.XrmTooling.CoreAssembly
  • Microsoft.Azure.Services.AppAuthentication
  • Microsoft.Azure.KeyVault

Step 3: Create an application account and app registration for Dynamics 365

To access Dynamics 365, you need an application user account, and you also need to register your dynamics 365 apps with the user account. This will require an article on its own, so I will share the link that contains step by step for setting up the account from Paul Nieuwelaar - Click here

Step 4: Create Azure SQL Database

If you don't have a database created already you can follow the link shared to do so. You can create your own tables instead of using order and order items which I have decided to use in this article - Click here

Step 5: Connect to Azure SQL

To connect to Azure SQL, we will use the SqlConnection class. In your console application, add the following code to connect to Azure SQL.

Create appseeting.json config file and add your conn string.

I created a DAL class and I am connecting to my Azure SQL within the class constructor.

Note: Make sure to replace the placeholders with your actual server's name, database name, username, and password.

Step 6: Query the data from Azure SQL

Once you have established the connection to Azure SQL, you can retrieve the required data. In this example, we will be fetching order records and related order items. Note that I am using dapper to save my data into a list. Create an order model as well. The following code can be used to fetch the data:

Note: The SQL query should be adjusted based on your table and column names.

Step 7: Connect to Dynamics CRM

To connect to Dynamics CRM, we will use the CrmServiceClient class. Add your connection string in app. config or appsseting.json file. Add the following code to connect to Dynamics CRM:

Note: Make sure you have an app. config file set up to access Dynamics.

Step 8: Save the data to Dynamics CRM

Once you have fetched the data from Azure SQL and established the connection to Dynamics CRM, you can create records in Dynamics CRM. In this example, we will be creating orders and related order items in Dynamics CRM. The following code can be used to create records:

This code works perfectly if you have followed everything outlined from the beginning.

Step 9: Deploy as a web job

To deploy this as a WebJob, follow C# design patterns and solid principles. It will help you to write a clean readable and you can always add more functionality to it. You will need more than what you see on the screen for a client project.

Note: C# Design patterns and Principles can be found on the following links.

Solid Principles, Design Pattern.

Step 10: Results

Once your WebJob is deployed and running you can check your logs.

Note. This is a sample code taken from the original code with more details. I hope this article helps someone in future to have an easy start when developing their first WebJob. Any advice or comment or suggestion will be taken into consideration. I am here to learn and share.

Happy Coding!!!

Keep reading

More thought leadership.

Data & Analytics2 min read

SQL Server 2016 End of Support: The Front Door to Azure Modernisation

SQL Server 2016 went out of support on 14 July 2026. Greg Fibiger on why the real blocker is rarely the database engine, what each of the remaining paths actually costs you, and how to avoid an expensive false start.

Read article
DevOps & Agile1 min read

Scope It or Sink It

Projects go sideways for one of two reasons: nobody scoped them properly, or nobody ever finished scoping them. Adrian Griesel on the real cost of planning too little, and why plans still come with an expiry date.

Read article
AI3 min read

Why AI came for code first, and who is next

Software leaves a trail of proof behind it. That trail explains why AI became good at code first, and when it arrives in your industry.

Read article

Get in touch

Got a problem worth solving?
Let’s talk.

Send us the gist of what you are trying to do. The right person from First Digital will be in touch within a working day.