Restful Service Integration in AX 365



This tip discusses how to consume external services inside AX 365. In this post, I am using channel advisor restful service to be consumed from Microsoft Dynamics 365. This will also help you know about calling GET, POST of restful service.


I got a requirement of importing sales orders from channel advisor in AX , but after researching many websites, I haven’t got a proper solution. I got everything in bits and pieces, so I thought of writing everything in one article.


Create an account on “” for application key, secret key and refresh token.

Using the Code

  1. Create a new Class Library project in Visual Studio, and name it CAIntegration as shown in the screenshot:

  2. Create a Class Generateaccesscode.cs as shown below:
    public string GetAccessTokenByKey()
 string content, accesstoken;
 var bytes = Encoding.UTF8.GetBytes
 ("[application id]:[secret key]"); 
 var base64 = Convert.ToBase64String(bytes);
 var client = new RestClient("");
 var request = new RestRequest(Method.POST);
 "application/x-www-form-urlencoded", ParameterType.HttpHeader);
 request.AddParameter("Cache-Control", "no-cache", ParameterType.HttpHeader);
 request.AddParameter("Authorization", "Basic " + base64, ParameterType.HttpHeader);
 "grant_type=refresh_token&refresh_token=[RefreshToken]", ParameterType.RequestBody);
 IRestResponse response = client.Execute(request);
 content = response.Content;
 string json = content;
 XmlDocument doc = 
 (XmlDocument)JsonConvert.DeserializeXmlNode(json, "Channeladvisor");
 XmlElement root = doc.DocumentElement;
 XmlNodeList nodes = root.SelectNodes("//Channeladvisor");
 accesstoken = "";
 foreach (XmlNode node in nodes)
 accesstoken = node["access_token"].InnerText;
 return accesstoken;

  3. Create another class for getting sales order from channel advisor by passing access token above methods.
    public string GetAllOrders(string accesstoken)
 string xml;
 var client = 
 new RestClient
      ("" + accesstoken);
 var request = new RestRequest(Method.GET);
 IRestResponse response = client.Execute(request);
 var content = response.Content;
 string json = content;
 XmlDocument doc = 
      (XmlDocument)JsonConvert.DeserializeXmlNode(json, "Channeladvisor");
 xml = doc.InnerXml.ToString();
 return xml; 

  4. Deploy the class library.
  5. Create a new Dynamics project in Visual Studio as shown in the screenshot.

  6. Add CAIntegration.dll as a reference.
  7. In the X++ class, you can use the external web services that were referenced in CAIntegration.dll.
    (wfsChannelAdvisor, FormButtonControl1), FormControlEventType::Clicked)]
    public static void FormButtonControl1_OnClicked(FormControl sender, FormControlEventArgs e)
    { Notes getOrdersXML; CAIntegration.GenerateAccessCode accessCode = new CAIntegration.GenerateAccessCode(); String255 accessCode1 = accessCode.GetAccessTokenByKey(); info(strFmt("Access code %1",accessCode1)); CAIntegration.Orders orders = new CAIntegration.Orders(); getOrdersXML = orders.GetAllOrders(accessCode1); info(strFmt("orderxml %1",getOrdersXML));
    } ...


  • 28th July, 2017: Initial version