I'm Joris "Interface" de Gruyter. Welcome To My

Code Crib

10-Minute AX 2012 App: WPF

Aug 31, 2011
Filed under: #daxmusings #bizapps

Powerfully simple put to the test. In this first article, we will create a WPF (Windows Presentation Foundation) application, which will query AX by consuming the system services. The system services are services that provide access to the AX metadata, queries, etc. We will achieve this in 10 minutes (or less… post your time in the comments!).

Ingredients:

  • Visual Studio 2010
  • Access to an AX 2012 AOS

To get started, open Visual Studio 2010 and create a new project. Under C# / Windows select the “WPF Application” project type. I named this project “DAXMusings.AXWPF”.

So, next up, we want to add a service reference to our AX WCF system query service. To do this, you will need to find out what address/port your AOS uses for the WSDL. On a default install, the WSDL address will be http://AOSSERVERNAME:8101/DynamicsAx/Services/QueryService . You can test this by just typing in that URL into a browser and see if you get the XML schema definition of the service (WSDL stands for Web Service Definition Language). If you don’t, you can find out what the actual port number is by opening the AX 2012 Server Configuration Utility. The first tab (“Application Object Server”) will show the WSDL port. Alternatively, you can open the AX client and navigate to System Administration > Setup > Services and Application Integration Framework > Inbound Ports. You won’t find the QueryService in there, but if you look at one of the other services such as AxClient, it will tell you what the WSDL URL is. Just replace the “AxClient” with “QueryService”.

Once you figured out your WSDL URL, go back to Visual Studio and right-click on the “References” node in your project, and select “Add Service Reference”.

Enter your URL, and hit “GO”. This will fetch the WSDL and show you what it has in store. Just enter a namespace (I’m using “QueryService” here, we’ll use this in code so you may want to follow me on this name) and click OK.

Next, we’ll add a DataGrid to the screen. In the XAML file, add one line to add the control. We’ll name it “MyDataGrid” (which is pretty irrelevant for this code), and we set its ItemsSource to “{Binding}”.

Lastly, we’ll add some code in the MainWindow.xaml.cs which should have opened by default when you created the project. We’ll add some code in the MainWindow() method.

Before I explain all that’s going, and before I’ve passed the 10 minute mark blabbing about the details, give it a spin. If you followed to the letter and your URL is correct, you should see a data grid with all the query’s fields, and whatever rows of data you have in your AX environment.

Click. What’s your time? :-)

So, the AX QueryService returns a standard System.Data.DataSet .NET object. The DataGrid control has the magic. First, we set it to Binding. Notice that we didn’t tell it WHAT to bind to. This is WPF feature where certain properties get passed down from parent controls to child controls (looking at the XAML file, the Window is the root, Grid is the first child, DataGrid is the child of Grid). So, from our code, we set “this.DataContext”, where “this” is the window. So the Window passes down the DataContext to all its children (see this MSDN article for more information on that). DataGrid also has a property called “AutoGenerateColumns”, which is set to true by default (so we don’t explicitly set it in the XAML). The feature will auto generate columns for each column contained in the dataset. Also note the “Paging” object. We use positionbased paging, which requires an explicit sort on the query. So if you try to use a different query, you may see the following error: “Paging is not supported for queries that do not have an ordering property”. You can pass in a NULL reference instead of creating an instance, however, some queries return so much data it will hit the maximum buffer size of your WCF service consumer. You can change these buffer sizes in your app.config file which is contained in your project.

There’s more you can do here of course. For one, you can hook up the paging object to some controls so you can actually page through the results. You could explore some of the other system services, for example to get a list of queries in the system, and then allow the user to select one and get the results.

No was that powerfully simple or what?!

 

There is no comment section here, but I would love to hear your thoughts! Get in touch!

Blog Links

Blog Post Collections

Recent Posts