Click here to Skip to main content
15,881,812 members
Articles / Programming Languages / X++
Tip/Trick

Restful Service Integration in AX 365

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
30 Jul 2017CPOL 12.6K   130   3  
How to consume RESTful service of channel advisor in AX 365

Introduction

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.

Background

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.

Prerequisite

Create an account on "https://developer.channeladvisor.com/the-developer-console" 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:

    Image 1

  2. Create a Class Generateaccesscode.cs as shown below:
    C#
    public string GetAccessTokenByKey()
            {
                string content, accesstoken;
                //For getting access token by using refresh_token
                //var path = Path.GetTempPath();
                var bytes = Encoding.UTF8.GetBytes
                            ("[application id]:[secret key]"); // application id:secret key
                var base64 = Convert.ToBase64String(bytes);
                Console.WriteLine(base64);
                var client = new RestClient("https://api.channeladvisor.com/oauth2/token");
                var request = new RestRequest(Method.POST);
                request.AddParameter("Content-Type", 
                                     "application/x-www-form-urlencoded", ParameterType.HttpHeader);
                request.AddParameter("Cache-Control", "no-cache", ParameterType.HttpHeader);
                request.AddParameter("Authorization", "Basic " + base64, ParameterType.HttpHeader);
                request.AddParameter("application/x-www-form-urlencoded", 
                "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;
                    //listBox1.Items.Add(node["game"].InnerText);
                    Console.WriteLine(node["access_token"].InnerText);
                }
                return accesstoken;
            }...
  3. Create another class for getting sales order from channel advisor by passing access token above methods.
    C#
    public string GetAllOrders(string accesstoken)
            {
                string xml;
                // for orders
                var client = 
                new RestClient
                   ("https://api.channeladvisor.com/v1/orders?access_token=" + 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.

    Image 2

  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.
    C#
    [FormControlEventHandler(formControlStr
    (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));
    } ...
    

History

  • 28th July, 2017: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Saudi Arabia Saudi Arabia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --