What if your lead asks you to integrate Dynamics AX with your Mobile application? In that case you need to be smart. If you have worked with WCF, you would be having an idea that when you host WCF you get a WSDL link which you can use to add a service reference.

Dynamics AX has AIF (Application Integration Framework) which is used to integrate Microsoft Dynamics AX with external software systems.

MSDN says,

Now issue here is that I need to integrate different tables of Microsoft Dynamics and need to Get and Post data to them. I have two options here,

  1. I might create a WCF and host that to communicate with my Mobile Application that may be Android, iOS or Windows Phone & Store.
  2. I might create ASP.NET Web API and communicate with my Mobile Application.

I preferred ASP.NET Web API on WCF, here are some of the reasons:

WCF ASP.NET Web API
Enables building services that support multiple transport protocols (HTTP, TCP, UDP, and custom transports) and allows switching between them. HTTP only. First-class programming model for HTTP. More suitable for access from various browsers, mobile devices etc enabling wide reach.
Enables building services that support multiple encodings (Text, MTOM, and Binary) of the same message type and allows switching between them. Enables building Web APIs that support wide variety of media types including XML, JSON etc.
Supports building services with WS-* standards like Reliable Messaging, Transactions, Message Security. Uses basic protocol and formats such as HTTP, WebSockets, SSL, JQuery, JSON, and XML. There is no support for higher level protocols such as Reliable Messaging or Transactions.
Supports Request-Reply, One Way, and Duplex message exchange patterns. HTTP is request/response but additional patterns can be supported through SignalR and WebSockets integration.
WCF SOAP services can be described in WSDL allowing automated tools to generate client proxies even for services with complex schemas. There is a variety of ways to describe a Web API ranging from auto-generated HTML help page describing snippets to structured metadata for OData integrated APIs.
Ships with the .NET framework. Ships with .NET framework but is open-source and is also available out-of-band as independent download.

Once we are done with developing our ASP.NET Web API we are going to host that using IIS Server and use public IP to let our mobile application communicate with our Microsoft Dynamics AX through Web API.

Being a mobile guy I don't have much idea about Dynamics AX. It took me few hours to understand Dynamics AX and in this tutorial I would start from scratch as if you never interacted with Dynamics AX previously.

So, let's start,

Let's start with a simple table, I would first create a simple table having not more than three attributes,

  1. ID
  2. First Name
  3. Last Name

And name the table as STUDENT.

We need to create a Unique Index field as well because if we are targeting READ and FIND method of Document Service.

Here is mine,

Code:

  1. public void Post([FromBody]Person value)
  2. {
  3. ServiceReference2AxdStudentQuery _TableQuery = new ServiceReference2AxdStudentQuery();
  4. ServiceReference2AxdEntity_StudentsTable_1 _StudentTable = new ServiceReference2AxdEntity_StudentsTable_1();
  5. _StudentTable.Name = value.Name;
  6. _StudentTable.ID = value.ID;
  7. _TableQuery.StudentsTable_1 = new ServiceReference2AxdEntity_StudentsTable_1[1] { _StudentTable };
  8. ServiceReference2StudentQueryServiceClient _Client = new ServiceReference2StudentQueryServiceClient();
  9. ServiceReference2CallContext _callContext = new ServiceReference2CallContext();
  10. _callContext.Company = "USMF";
  11. ServiceReference2EntityKey[] entityKeys = _Client.create(_callContext, _TableQuery);
  12. }
Now what you need to do is to start that Web API in Google Chrome as it renders JSON more easily. I am using JSON as format of data so I need to add two lines to change that form XML to JSON. Add these lines in WebApiConfig.cs file

Code:
  1. var appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == "application/xml"); config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType);
To test it you just need to post to the URL of your Web API using any of the tool. I personally use Fiddler. When you would open Table in Table Explores you would notice that your POST have added data respectively to Dynamics Student Table. We are done with your Integration, Now next part here is to integrate that with mobile.

All the work I have done here is in a server environment. To use that Web API I need to host that using IIS Server. Here a blog that you can follow.

Once you have hosted your Web API, you would be having a URL that you may use to hit that.

You can consume that API on any of the platform that supports JSON. e.g. Windows, Android, iOS or any platform including JAVA.

Let's try to look at the procedure to consume that in Universal Windows Platform Application.

Whenever you are parsing JSON you need to request data using Client. Well it's same for parsing Json.net way or native way.

Code:

First you need initialize client to request JSON data,
  1. var client = new HttpClient();
Then for obvious reasons you need to put request URL for JSON data,

Code:
  1. HttpResponseMessage response = await client.GetAsync(new Uri("http://YourserviceURL"));
It's time to get JsonString so that we can parse that,

Code:

var jsonString = await response.Content.ReadAsStringAsync();

Now here things become a bit tricky, my json is consisted to simple JSONArray, your JSON may appear a bit different from what my json looks like. Here is a way to know how your JSON looks like. Just navigate to json2csharp.com, put the url of JSON and it would return you with respective C# class of your JSON. You may use that to cast you JSON object.

As my JSON is a simple array so I just need to cast that to JSONArray and get out objects from that. I have saved my JSON respective C# class as "RootObject"

Code:
  1. JsonArray root = JsonValue.Parse(jsonString).GetArray();
  2. for (uint i = 0; i < root.Count; i++)
  3. {
  4. //Parse that accordingly
  5. });
Here obs_mydata is my ObservableCollection where I am adding channels, whereas RootObject is my JSON respective C# class. Now you just need to bind your Observable Collection with UI. I would strongly suggest you to follow MVVM pattern and get full out of BLEND for VS.

To get respective C# class of your JSON you can use an online tool.

I hope you would have got some idea from this article about integrating Dynamics AX with your Mobile App of any other platform.

Credits: Some of the documentation of MSDN has been used as part of blog.