Tired of creating a new console application to connect to Dynamics 365 CRM/Dataverse? Use a template!

Sometimes we are asked to create console applications that interact with Dynamics CRM/ Dataverse, and like every new developer, we create a new project and look for a piece of code on the internet to start, or we copy existing code from an old project. I used this approach during all my projects, today I say stop, I want something more automatic and practical to start any new project of this kind.

Today I will share with you my experience on how to create a console app template project to connect dataverse and how to share the project template using a visual studio extension.


Let’s start with a demo. After following the steps in this blog, you will have the possibility to create a console app project that connects to Dataverse/Dynamics 365 CRM. The objective is to start implementing the business logic of the application without worrying about other aspects of configuration and services instantiation.

After creating the project, we can directly start implementing the business logic of the application:

My application uses system environment variables. After the correct configuration of these variables. We can launch the console application:


How to create this project template?

The creation of a project template is done from an existing project. After implementing your first console app, Visual Studio allows us to export it as a template.

Go to Project –> Export Template …

Choose the template type as a project template and choose the source project:

Next, simply fill in the form in the export wizard:

Now, we can use this template when creating a new project:

Also, we can share this project model with other developers using a VisuaAlso, we can share this project model with other developers using a Visual Studio extension. To do this, we must first install the Visual Studio SDK using the installer. Once done, we can create a VSIX project:

After creating the project, you need to copy the ZIP of the previously generated model to the VSIX project.
The ZIP of the template will be found on the following path: %USERPROFILE%\My Documents\Visual Studio {version}\My Exported Templates\

You will find more details on the steps to follow on the Microsoft docs

Then, right-click on the ZIP file. Then property and change the option “Copy to Output Directory” to “Copy Always”.

Finally, the manifest needs to be configured:

Metadata:

Assets: add the type Microsoft.VisualStudio.ProjectTemplate and set the path to the template which is the ZIP file we added at the beginning.

After building the project. A vsix file will be generated. Close Visual Studio and go to the /bin/debug folder and launch the installation of the extension.

After installing the extension, we will be able to create a new project of type console application that will connect to Dataverse server. Please feel free to download my extension on this link.


How to use this project model?

After creating a new project from the “Dataverse and Dynamics 365 CRM Console App Template”. We notice that several files are created.

Please note that you will need to restore NuGet Packages

Then, launch the application. You will notice that the console stops immediately because it cannot establish a connection to the Dataverse server.

To establish the connection, you only need to configure the following environment variables:

  • CRM_SERVER_URL: represents the Dataverse/Dynamics 365 CRM server url. Example: https://org5f765028.crm.dynamics.com
  • CRM_CLIENT_ID: represents the id of the application registered on Azure.
  • CRM_CLIENT_SECRET: Represents the secret key to connect to the Azure app.

After the correct configuration, we can focus on the business logic to be implemented on the Console app. All the logic of the application will be done at the AppWorker class.

Please note that you can find the project template on github.

hope it helps …

Advertisement

2 thoughts on “Tired of creating a new console application to connect to Dynamics 365 CRM/Dataverse? Use a template!

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 )

Connecting to %s