Dynamics AX 2009

The code added for User Controls can access X++ code. To do this, you must use a proxy. This makes the X++ code resources available for use in the Visual Studio project. The following can be made available through the proxy:

  • Classes

  • Methods (can be part of a class or a table)

  • Enums

A proxy has already been defined for Microsoft Dynamics AX. If you have additional classes, methods, or enums that you want to access from User Controls, you will add them to this existing proxy.

Define the classes, methods, and enums as you typically would for X++ code. After the code has been defined, you will add references to the Proxies static file that is maintained in the AOT.

To add to the Microsoft Dynamics AX proxy

  1. In the AOT, expand the Web node.

  2. Expand the Web Files node.

  3. Expand the Static Files node.

  4. Right-click the Proxies file, and then click Edit.

  5. Use the Web Application Editor to add definitions for the additional items that you want to include in the proxy. Typically, additional items are added at the end of the file. You can use the existing items in the proxy as a template for how to add more items.

    To add an enum, use the following syntax.


    To add a class that has methods, use the following syntax.


    To add a table with methods, use the following syntax.

  6. When you have finished making changes, close the Web Application Editor. Be sure to save the changes.

After the proxy file has been updated with new content, it must be deployed so that it can be used.

To deploy the updated proxy

  1. In Microsoft Dynamics AX, click Microsoft Dynamics AX > Tools > Development Tools > Web development > Proxies.

  2. In the Generate Proxies window, click Generate in the Development Web Site group. After several moments, the proxies will be generated for the Enterprise Portal Web site.

To reference the proxy from a Visual Studio project, you must generate the C# files that describe the items in the proxy.

To generate C# files for the proxy

  1. Open the Visual Studio project for your User Control.

  2. In Solution Explorer, right-click the App_Code group. Click Generate Proxies to create the C# files that provide the interface for the proxy.

  3. To view the C# files generated, expand the Proxies folder in the App_Code group. View the individual files to see details about what each file accesses.

To use the proxy from C# code for the User Controls, you must add a reference in your code. The following using statement makes the content of the proxy available to your code.

using Microsoft.Dynamics.Portal.Application.Proxy;

After you have added the using statement, the C# code for the User Control can use the items defined in the proxy. Some items in the proxy such as enums can be accessed directly without any additional code. Accessing methods from the proxy requires passing the IAxaptaAdapter object together with the method call. This object is part of the AxSession object. Add the following helper method to the C# file for the User Control in your Visual Studio project to allow for the AxSession object to be retrieved.

private ISession AxSession
        AxBaseWebPart webpart = AxBaseWebPart.GetWebpart(this);
        return webpart == null ? null : webpart.Session;

When you call the method from the proxy, use this helper method to retrieve the IAxaptaAdapter object. The following example calls the CalculateGrade method from the Test class. Notice how the IAxaptaAdapter object is retrieved using the AxSession helper method.

protected void Button_Calculate(object sender, EventArgs e)
    string test_result;
    using (IAxaptaAdapter a = this.AxSession.AxaptaAdapter)
        test_result = Test.CalculateGrade(a, Convert.ToInt16(Score.Text));
        Grade.Text = test_result;

Notice that a using block encloses the code that works with the IAxaptaAdapter. Use this pattern to make sure that the resources for the IAxaptaAdapter will be freed when it goes out of scope.

Community Additions