With the increasing scope of Microsoft Dynamics 365 Finance and Operations in the ERP Industry an efficient requirement is to perform its integration with other applications.
One basic example is to perform an Integration with a JSON REST Service through a published WebAPI.
In the below example I have done the integration with Dynamics CRM where I am sending some values to CRM from Operations :-
The Code goes like this :-
int find;
real inventOnHandQtyval;
str url,aosUri,activeDirectoryTenant;
str activeDirectoryClientAppId;
str activeDirectoryClientAppSecret,json;
str postData,activeDirectoryResource;
str aadClientAppSecret,oAuthHeader;
str prdGUIDCode,prodnum;
str returnValue,jsonString,jsondszstr;
System.Net.HttpWebRequest request;
System.Net.HttpWebResponse response;
System.Byte[] byteArray;
System.IO.Stream dataStream;
System.IO.StreamReader streamRead;
System.IO.StreamWriter streamWrite;
System.Net.ServicePoint servicePoint;
System.Net.ServicePointManager servicePointmgr;
System.Net.HttpVersion version;
System.Web.Script.Serialization.JavaScriptSerializer jsonSerializer;
CLRObject clrObj;
Newtonsoft.Json.JsonReader reader;
Newtonsoft.Json.Linq.JObject prod;
System.Text.Encoding utf8;
System.Exception ex;
Counter countCounter;
Object obj;
Map data;
EcoResProduct ecoresproductfetch;
EcoResProductTranslation ecoresproductTranslation;
InventTableModule inventTableModule;
try
{
InventTable inventtable = sender as InventTable;
System.Net.WebHeaderCollection headers = new System.Net.WebHeaderCollection();
var jsonSerializerobj = new System.Web.Script.Serialization.JavaScriptSerializer();
prod = new Newtonsoft.Json.Linq.JObject();
inventOnHandQtyval = InventOnhand::newItemId(inventtable.ItemId).availPhysical();
select inventTableModule where inventTableModule.ItemId==inventtable.ItemId;
prod.Add("name",inventtable.itemName());
prod.Add("productnumber",inventtable.ItemId);
prod.Add("defaultuomscheduleid@odata.bind","/uomschedules(7F6F7338-1D80-4E90-9110-A70897C73834)");
prod.Add("defaultuomid@odata.bind","/uoms(68A2E342-E1CA-4CC2-B430-C05057BCE7BC)");
prod.Add("currentcost",0.00);
prod.Add("description",inventtable.itemName());
prod.Add("msdyn_fieldserviceproducttype",690970000);
json=Newtonsoft.Json.JsonConvert::SerializeObject(prod);
System.IO.MemoryStream mem = new System.IO.MemoryStream();
new InteropPermission(InteropKind::ClrInterop).assert();
headers = new System.Net.WebHeaderCollection();
url = "https://abc.crm4.dynamics.com/api/data/v8.1/details";
clrObj = System.Net.WebRequest::Create(url);
aosUri = "https://login.windows.net/abcd1234/oauth2/authorize?";
activeDirectoryTenant = "https://login.microsoftonline.com/abcd134/oauth2/token";
activeDirectoryClientAppId = "xyz124";
activeDirectoryResource = "https://abc.crm4.dynamics.com";
Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext authenticationContext =
new Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext(activeDirectoryTenant);
var userCredential = new Microsoft.IdentityModel.Clients.ActiveDirectory.UserCredential("abc@xyz.onmicrosoft.com", "abc@1234");
Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationResult authenticationResult =
authenticationContext.AcquireTokenAsync(activeDirectoryResource, activeDirectoryClientAppId, userCredential).Result;
headers.Add("Authorization", authenticationResult.CreateAuthorizationHeader());
headers.Add("Prefer", "return=representation");
request = clrObj;
request.Headers=headers;
utf8 = System.Text.Encoding::get_UTF8();
byteArray = utf8.GetBytes(json);
request.set_Method("POST");
request.set_KeepAlive(true);
request.ContentType="application/json";
request.set_ContentLength(byteArray.Length);
servicePoint = request.get_ServicePoint();
System.Net.ServicePointManager::set_Expect100Continue(false);
System.Net.ServicePointManager::set_SecurityProtocol(System.Net.SecurityProtocolType::Tls12);
dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.get_Length());
response = request.GetResponse();
dataStream = response.GetResponseStream();
streamRead = new System.IO.StreamReader(dataStream);
jsonString = streamRead.ReadToEnd();
data=RetailCommonWebAPI::getMapFromJsonString(jsonString);
prdGUIDCode = data.lookup("productid");
prodnum = data.lookup("productnumber");
}
catch(Exception::CLRError)
{
ex = CLRInterop::getLastException().GetBaseException();
error(ex.get_Message());
}
dataStream.Close();
response.Close();