In Microsoft Dynamics 365 we always need to integrate with
other software or application.
Most of integration need custom or built-in connector to
send and receive data from Dynamics CRM.
In almost each integration we need web API to sent and
receive request. This mainly consider through server-side code or scripting.
But in some cases, we need to implement client-side
scripting to use our web API.
Use Case: -
We have created a Rest based web API in
our local system. And it works fine in local environment. We need to call our local
Web API from Dynamics CRM.
We need to tackle cross-browser request-response issue. We
should ale to call our web API from client-side scripting like “JavaScript”.
We can also deploy our Web API in Intranet organization. We can
also deploy our web API in Azure portal or other portals, so it can also
support server-side. But in this article, I am not considering Server-side API consumption.
Prerequisite: -
- Visual Studio (Example on VS 2013)
- Internet connection
- Dynamics CRM instance (online or on-premise)
Steps: -
- Create a Web API.
- Handle Cross-platform access
- Calling Web API from local web application
- Calling Web API from Dynamics CRM.
Let Start!!
Create a Web API: -
Open
Visual studio and create and click on new project select “ASP.NET web
Application” from template.
Select “Empty” template and check “Web API” from add folder
and core reference folder.
Open package manager console and install Cors package to
handle cross-platform.
Install-Package Microsoft.AspNet.WebApi.Cors
Now Add new controller class named “TestController”.
Add below code in
your class.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Web.Http;
using System.Web.Http.Cors;
namespace LocalWebAPI.Controllers
{
[EnableCors(origins: "*", headers: "*", methods: "*")]
public class TestController : ApiController
{
string fileName = @"D:\demo.txt";
public HttpResponseMessage Get()
{
try
{
//
Check if file already exists. If yes, delete it.
if (File.Exists(fileName))
{
File.Delete(fileName);
}
//
Create a new file
using (FileStream fs = File.Create(fileName))
{
// Add some text to file
Byte[] title = new UTF8Encoding(true).GetBytes("Demo Text File");
fs.Write(title, 0,
title.Length);
byte[] Time = new UTF8Encoding(true).GetBytes(" " + DateTime.Now.ToLongTimeString());
fs.Write(Time, 0,
Time.Length);
}
}
catch (Exception Ex)
{
}
return new HttpResponseMessage()
{
Content = new StringContent(DateTime.Now.ToLongTimeString())
};
}
public HttpResponseMessage Post()
{
return new HttpResponseMessage()
{
Content = new StringContent("POST: Test message")
};
}
public HttpResponseMessage Put()
{
return new HttpResponseMessage()
{
Content = new StringContent("PUT: Test message")
};
}
}
}
You can see we have added below code on the top of class to
allow all request from any system. Later we will restrict to our Dynamics CRM
application only.
[EnableCors(origins: "*", headers: "*", methods: "*")]
Now open “WebApiConfig.cs” class and add below code.
config.EnableCors();
Now our Web API is ready to test and use. Press F5. It will
open forbidden page.
Add”/api/test”
after URL to test your Web API.
Example: -
You can see now output as current date. You can also check
local file that create by our Web API at specified location.
I.e we can also use File operation in our Web API.
Calling Web API from local web application: -
Open
Visual Studio and create Asp.Net or HTML application.
Add a HTML page in your application and add below code.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript">
var xmlHTTP;
if (window.XMLHttpRequest) {
xmlHTTP = new window.XMLHttpRequest;
}
else {
try {
xmlHTTP = new ActiveXObject("MSXML2.XMLHTTP.3.0");
}
catch (ex) {
}
}
xmlHTTP.open("GET", "http://localhost:51453/api/test", true);
xmlHTTP.onreadystatechange = function () {
if (xmlHTTP.readyState == 4 && xmlHTTP.status ==
200) {
alert(xmlHTTP.responseText);
}
}
xmlHTTP.send();
</script>
</head>
<body>
</body>
</html>
Replace URL with your local host URL. Now run your HTML page
and you will able to call your Web API.
Make sure your previous Web API application is in running
mode.
Calling Web API from Dynamics CRM: -
Login
in your Dynamics CRM organization and create a solution and add HTML web
resource that we have already created.
Click on “PREVIEW” button to test your Web API.
If everything working fine. Now time to restrict to WEB API
call from to accept only our Dynamics CRM call instead of all.
Stop your Web API application and replace “*” with your
Dynamics CRM URL in “TestController”
Class.
If you want that you Web API can access from all user in your
Intranet network. Than publish our Web API any local server IIS which should be
accessible in your Intranet network.
Limitation: - These web API and not accessible from
server-side. So, you will not able to use this Web API in plugin or custom
workflow.
You can enhance this functionality based on your
requirement.
Please comment if faced any trouble while implementation.
Click on follow button to be updated about my blogs.
HAPPY CRM 😊!!
No comments:
Post a Comment