In this series, I’m investiging alternate ways to make a connection from an external website to the back-end Dynamics CRM system. In the previous article in this series, I posted some code snippets of the proof of concept I was working on.
In this article, I’ve written a proof of concept that actually connects from your external website to Dynamics CRM using Knockout.js and a custom Authentication Service.
The custom authentication service, takes the call you want to make to CRM, puts authentication to the call and forwards it to Dynamics CRM. The results are passed as is in JSON format back to the calling website.
How does it work?
My goal was to make a proof of concept. This code is not yet ready for production.
As soon as the web project is launched, a web page appears with a Select link in it. When you click the link, the screen is populated with data stored in CRM.
As soon as I click the select link, the knockout viewmodel triggers a REST call to the Authentication Service. In that REST call, the actual CRM webservice call is passed as argument. For that particular reason I implemented the call to the Authentication service as an Http POST request.
Once the call is received by the Authentication Service, the posted data is captured. The posted data is forwarded to CRM using the C# WebClient object. The WebClient object gets the user credentials of the user that is allowed to make connection to CRM (see code below).
In order to receive the results as a JSON string, the WebClient is enriched with additional headers. To get the data from CRM (Http GET) we have to use the DownloadString(query) method. Once the JSON data is received, it is converted to a byte array which is streamed back to the calling website.
What needs to be done?
As I mentioned before, the code I provide is a proof of concept. For that reason I implemented a GET method to get data from CRM. If you want to be able as well to POST data to CRM you’ll have to add a POST method (which is from a javascript perspective the same logic. The C# WebClient objects needs to call a different method to post the data.
I also implemented the Authentication Service as a console application. That has to be changed as well, e.g. write a Windows Service or a Web service to host the functionality.
Source code
For your convenience I put up the File Attachment: KnockOutCRM.zip (54 KB). This file contains the web project. Furthermore I put up the File Attachment: AuthenticationService.zip (6 KB). This file contains the authentication service.
In order to run the Authentication Service, you have to put valid credentials (user name, password, domain) in the program.cs file. The Authentication Service has to be run with elevated privilleges (run as administrator), otherwise the HttpListener will throw an Access Denied exception.
In the web project I hardcoded the URL of my development CRM server. You’ll have to change that URL. Furthermore I refer to an existing item in my Contacts entity by using a specific guid.
For now I’ve met my goal!
I’m able to connect to Dynamics CRM from an external website. If you want me to finish the Authentication Service, please let me know.
Thank you for reading.