Developing Applications for Microsoft Dynamics AX with X++ and the .NET Framework

Introduction

Microsoft's Dynamics AX is an ERP system (Enterprise Resource Planning) in the traditional sense: it contains tons of features and usually requires customizations to best support an organization's needs, and can initially look like a black box. But every system has its internals, and for developers, Dynamics AX is a challenge that allows you to get started without much sweat.

Since many .NET developers already have access to Microsoft MSDN subscriber downloads, it is worthwhile to check what is being offered by this subscription. One of the benefits is a developer version of Dynamics AX. This is a limited-functionality version that is suitable for testing and development work, but not production.

The purpose of this article is to show you how to get started with the MSDN version, and how to write basic integration code for the system. The first step in this process is to get Dynamics AX running.

Installing Dynamics AX on a test server from MSDN

If you purchased Microsoft Visual Studio together with MSDN, log into MSDN subscriber downloads, and expand the Servers section. From there, you can find Dynamics AX 2009, which is the latest version at this writing. You can either download the original installation DVD along with a license file, or you can download a ready-made 14-part (yes, 14!) RAR compressed Hyper-V virtual machine with everything set up for you (Figure 1).


Figure 1

Starting the with ready-made trial version is probably the easiest option, especially if you haven't used Dynamics AX previously. To run the trial, you will need a 64-bit Windows Server 2008 R2 server with Hyper-V enabled. The RAR archive you downloaded contains a VHD virtual disk image and a configuration file. You can import these into Hyper-V, and after this, you are ready to run. For more details, consult the ReadMe file that comes along with the archive.

The first screen you will see in the virtual machine is the Windows login screen. The login password for the Administrator account is "pass@word1". If you have an U.S. keyboard, then this should be easy to type; however, if you have a non-U.S. keyboard, then the @ sign can be problematic. You can try to press Shift+2 to get the @ sign, and then logging in.

If nothing else helps, try changing the username, and try typing the password to the username field to see what characters you get. Then, try logging again as the Administrator. Alternatively, enable the on-screen keyboard, and use it to type the password. After logging in, you can change to the correct keyboard layout using Control Panel.

Once you see the desktop of the virtual machine, you will find an icon to launch Dynamics AX. Go ahead and start it. You will soon see the Dynamics AX main window (Figure 2), after which you could browse the available features to learn about Dynamics AX. Once you are done, it's time to dive into the development possibilities created by Dynamics AX.


Figure 2



Developing Applications for Microsoft Dynamics AX with X++ and the .NET Framework

Development options with Dynamics AX

If you are interested in developing applications for Dynamics AX, there are two broad categories of technology choices you can make to base your development on. Firstly, you can use Dynamics AX's own internal programming language called X++. This language allows you to program custom logic within the Dynamics AX system itself. However, because .NET development is the norm nowadays, Dynamics AX also allows you to write .NET framework code to build your own applications. Let's look at both of these options in turn.

The original technology used to develop custom solutions for Dynamics AX is the X++ programming language. This language has a familiar-looking syntax at least to all developers knowing a C-based language, such as C++, C# programming or Java. When you install Dynamics AX, you will also gain access to an integrated development environment with which you can write code.

The X++ language itself has syntax similar to the C language, but also contains object-oriented features. Development is usually done directly inside Dynamics AX, where the development environment hosts a compiler and a debugger. The compiler is also smart enough to give the developer optimization tips: for example poorly written SQL statements cause the compiler to display performance tuning tips.

X++ exposes a wide range of functions which can be used to integrate custom code with the system. You can access data inside the Dynamics AX database, create new user interface elements such as menus and forms, and more. Here's a quick glimpse of the X++ language showing the classic "Hello, World!" message:

static void main(Args args)
{
    ;
    info("Hello, World from X++ language!");
}

To test this code in Dynamics AX, you would need to add a new class, and additionally a new method called main into the class. Then, to display a message on the screen, you could for example use the global info function (Figure 3).

[Figure 3 - Info Message.png]
Figure 3

The alternative to X++ language development in Dynamics AX is to use .NET framework technologies. If you prefer to use .NET and languages such as C#, you can develop in two different ways: you can write libraries (DLLs) that Dynamics AX loads and runs, or you can use the Dynamics AX object model directly from your own .NET applications.

Developing a DLL to be used inside Dynamics AX

The first option to integrate .NET code and Dynamics AX is to use a feature called .NET CLR Interop (Common Language Runtime Interoperability). This feature allows you to develop a .NET DLL with C# and then use X++ code to call methods inside the DLL. One of the features in .NET CLR Interop allows you to call basic classes inside the .NET framework directly from X++. For instance, to get the current date by using the .NET class System.DateTime, you could use the following X++ code:

static void main(Args args)
{
    System.DateTime     now;
    str                 datestr;
    ;
    
    now = System.DateTime::get_Now();
    datestr = now.ToString();
    info("It is now: "+datestr);
}

Although a very simple example, the above code snippet shows that .NET is already deeply integrated into Dynamics AX 2009. However, to be able to use your own .NET code inside a custom DLL (assembly in .NET speak), you will first need to tell Dynamics AX where to find your assembly.

To be able to reference custom DLLs within Dynamics AX, you will need to learn about the Application Object Tree, or AOT. The Application Object Tree is the central location through which all objects, code, and user interface elements are kept. You can view the Application Object Tree in Dynamics AX by clicking the Application Object Tree button on the toolbar (Figure 4). This command opens the full tree, and since you are at this point interested in referencing custom DLLs, then scroll down the object tree to find the References node. On the keyboard, you can press Ctrl+D to get quick access to the object tree.

[Figure 4 - Show AOT Toolbar.png]
Figure 4

The References section lists all the .NET assemblies that the Dynamics AX system is aware of. To add your custom DLL to the list, you should first copy it to the Client\Bin folder under the Dynamics AX installation path. The path is usually C:\Program Files\Microsoft Dynamics AX\50\Client\Bin.

Once you have copied your custom DLL to this folder, the next step is to right-click the References node in the Application Object Tree, and choose the Add Reference command from the popup menu. In fact, this is just like you would do inside Microsoft Visual Studio (Figure 5).

[Figure 5 - Add Reference.png]
Figure 5

Once the reference is in place, you can start writing some X++ code. In the Application Object Tree, create for instance a new job under the Jobs node, and write for instance the following method to retrieve a string from the .NET DLL:

static void DotNetIntegration(Args _args)
{
    HelloDynamicsAxLibrary.HelloDynamicsAx  myClass;
    str  message;
    ;    
    myClass = new HelloDynamicsAxLibrary.HelloDynamicsAx();
    message = myClass.SayHello();
    info(message);
}

In this example, the X++ code first instantiates a .NET public class called HelloDynamicsAx, and then calls its public SayHello method (Figure 6). Of course, this is a simple example of returning just strings from .NET code to Dynamics AX, but you could very easily write more advanced functions with C# programming. At the end of the X++ listing, the info function shows a message on the screen. Remember that Dynamics AX was developed before .NET Framework 4.0 and Microsoft Visual Studio 2010 became available. Thus, the .NET runtime version hosted inside Dynamics AX is of version 2.0. However, you don't need to resort to developing your code with Microsoft Visual Studio 2005, because both Microsoft Visual Studio 2008 and 2010 support changing the target .NET framework version when you first create a project (or at later time from project properties). See Figure 7.

[Figure 6 - Call C# DLL.png]
Figure 6

[Figure 7 - Target .NET Version.png]
Figure 7



Developing Applications for Microsoft Dynamics AX with X++ and the .NET Framework

Calling Dynamics AX Functions from .NET Framework Applications

In the previous section, you saw how you can run .NET code inside Dynamics AX. However, using a technology called .NET Business Connector, you can call Dynamics AX functions from your .NET applications. On the Microsoft Visual Studio side, the .NET Business Connector is a DLL which you need to reference in your project. After the reference has been added, you can use the different classes exposed by the DLL.

To try out the .NET Business Connector, start a new Microsoft Visual Studio project (for instance, a Windows Forms or a WPF project), and then add a reference to the DLL named Microsoft.Dynamics.BusinessConnectorNet.dll into your project. Usually, this DLL is located in the path C:\Program Files\Microsoft Dynamics AX\50\Client\Bin. This is the same folder where you copied your custom .NET DLL in the previous section.

Once referenced, you will gain access to the object model of the .NET Business Connector. Using Microsoft Visual Studio's Object Browser window is a good way to check what's included in the DLL; for instance, you will gain access to classes such as Axapta and AxaptaRecord. These are defined in the namespace Microsoft.Dynamics.BusinessConnectorNet. Here is an example of reading database data using a simple block of C# code:

private void button1_Click(
    object sender, EventArgs e)
{
    Axapta app = new Axapta();
    app.Logon(null, null, null, null);
    MessageBox.Show("Logon succeeded!");

    AxaptaRecord records =
        app.CreateAxaptaRecord("InventItemPrices");
    records.ExecuteStmt("SELECT ItemId, Price FROM %1");

    int count = 0;
    while (records.Found)
    {
        object field1 = records.get_Field(3);
        object field2 = records.get_Field(5);
        MessageBox.Show(field1 + " = " + field2);
        records.Next();

        // show ten first records
        count++;
        if (count > 10) break;
    }

    // logoff and clean up
    records.Dispose();
    app.Logoff();
    app.Dispose();
    MessageBox.Show("Logoff succeeded.");
}

As you can see, the code is easy to follow. Furthermore, you can use traditional SQL statements to read (and later update) data inside the Dynamics AX database. In fact, a regular SQL Server instance hosts the data manipulated by a Dynamics AX installation.

Conclusion

Dynamics AX is an ERP application that can be extensively customized. Some ERP systems are more closed than others, but for Microsoft developers, the good news is that Dynamics AX easily opens to .NET developers. The only problem is that example material is scarce on the Internet.

In this article, you learned how you can extend the functionality of Dynamics AX, and how you can get started with developing .NET and C# solutions for Microsoft's own ERP system. If you have an MSDN subscription, then you can already download a free trial version of Dynamics AX, and start playing with the system.

Good luck with your Dynamics AX development efforts!



About the Author

Jani Jarvinen

Jani Jarvinen is a software development trainer and consultant in Finland. He is a Microsoft C# MVP, a frequent author and has published three books about software development. He is the group leader of a Finnish software development expert group at ITpro.fi and a board member of the Finnish Visual Studio Team System User Group. His blog can be found at http://www.saunalahti.fi/janij/. You can send him mail by clicking on his name at the top of the article.

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Targeted attacks and advanced threats are customized to infiltrate your unique IT infrastructure, evade conventional defenses, and remain hidden while stealing your corporate data. To detect these criminal intrusions, analysts and security experts agree that organizations should deploy advanced threat protection as part of an expanded security monitoring strategy. For this comparative analysis of breach detection systems, product analysis reports and comparative analysis reports are used to create the security …

  • A modern mobile IT strategy is no longer an option, it is an absolute business necessity. Today's most productive employees are not tied to a desk, an office, or a location. They are mobile. And your company's IT strategy has to be ready to support them with easy, reliable, 24/7 access to the business information they need, from anywhere in the world, across a broad range of communication devices. Here's how some of the nation's most progressive corporations are meeting the many needs of their mobile workers …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds