Extending Dynamics 365 with Bots: Lead Generation

Ever since Microsoft came up with an idea to closely integrate Office and Dynamics CRM & ERP (of course with Azure), we all witnessed its benefits and acceleration towards the business goals. As a Software Engineer / Consultant (mainly for Microsoft Stack), I have worked on different frameworks, products and technologies depending upon the business requirements ranging from classic ASP.NET to CRM 2011 to WPF to Bot Framework & Cognitive Services.

Lately, while working on Dynamics 365 CRM, I realized that there’s much more than Plugins and Custom Workflows. As I am passionate about the Bot Framework and its flexibility to work seamlessly with many different products (thanks to Azure), I decided to come up with the series of posts which will discuss the possibilities of enhancing customer experiences and increasing productivity. In this post, I am assuming you’re familiar with the basics of bot framework and how it works (101 probably).

I’m not touching the goodness of Power Platform this time but I will surely discuss them in my upcoming blog posts with some constructive business use-cases.

The Web API Challenge

When I was conceptualizing about all this new initiative of Microsoft Dynamics 365 with Bots, I was thinking that it would really be easier and organized as I would be using CRM SDK Assemblies which were just a NuGet package away! In fact, after brainstorming on the first use-case (which I will talk about later on), I directly jumped onto coding for retrieving some information from CRM. After my unit tests, I was all set to plug this library with my Bot and to finish the first case. Voila! Alas! It wasn’t this easy (read: simple).

Actually, I referenced the just baked CRM Library (.NET Framework 4.6.2) into my Bot’s project (ASP.NET Core) and as soon as the referenced method is called, I got an exception. That very exception became the reason of this whole story which I am writing down. In case you’re wondering about the exception, it was:

Could not load file or assembly ‘System.ServiceModel, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089’. The system cannot find the file specified.

After a quick search, I realized that .NET Core does not have the support for these libraries and as per the discussion on GitHub, they will not be supported either. Initially I tried to go with workaround and consulted a few experts but all of my tactics miserably failed. Then, I decided not to opt for any workarounds and I was left with;

  1. WCF – By utilizing the power of Organization Service (as it’s supported with .NET Framework 4.6.2)
  2. Web API – Best practice and recommendation from the experts on different forums

Therefore, I decided to go with the Web API. Now, this decision of course was correct and recommended but took a little time to set everything up. I will briefly write down the steps in case it’s your first time with Web API of Dynamics 365.

Note: It’s recommended that if you are creating plug-ins, custom workflow activities, or custom XAML workflows, use the SDK assemblies (.NET)

  1. In order to access Dynamics 365 from external source (or bots in our case), you need to build Server-to-Server (S2S) authentication. Other fellows have already written a lot about it.
  2. Once you’re done with the authentication, you can easily test it using Postman.
  3. After testing, it you can easily integrate them into your apps (or bots). For your reference, you can check out the code in my repository.

The Events Bot

Whether you’re working for a bank who’s looking for prospect customers or you’re in a real estate company, you need leads to work upon and turn them into opportunities. For the sake of real world business case, I took customer’s initial registration for an upcoming event.


First, after following all of the above, I used bot builder CLI tools to populate my BotConfiguration.Bot file with all the credentials and configurations required to access the Dynamics 365 CRM with Web API.

msbot connect generic --name "CRMCredentials" -keys "{""serviceUrl"":""https://yourowndynamicsurl.crm.dynamics.com/"",""clientId"":""<your app id>"",""redirectUrl"":""https://localhost"",""key"":""<your app key"",""authEndpoint"":""https://login.microsoftonline.com/<your app auth endpoint>""}"

Then, I created a simple class within my Bot’s project for Dynamics 365 CRM connectivity (authentication) and operations.

Later on, I crafted the flow of my bot using the Waterfall Dialog as the main concept is to show you how easy and convenient it is to work with Bots. I also used one of the custom AdaptiveCards integration for you.

RegistrationEventually, the flow is like, customer comes for the registration, enter her details and after that bot takes all that information to CRM to generate a lead.

Dynamics 365 Lead Generation

In case you want to learn more about it or you’re passionate to contribute in this project, I have pushed this on GitHub. Happy coding!

Related Posts