Skip to main content

Call your Ax customization from Visual Studio C# code

How about calling your Axapta customizations (Classes / Class & Table methods / Enums) from your EP User control?

For this you need your X++ code resources available for use in Visual Studio. A proxy can do it for you. Ax already has this proxy defined for you as Proxies static file that is maintained in the AOT. You need to add references for your classes, methods or enums that you want to access from User Controls to this existing proxy.

Step 1

Add to the proxy

  • In the AOT, Go to Web --> Web Files --> Static Files --> Proxies file
  • Right-click the Proxies file, and then click Edit.
  • Use the Web Application Editor to add definitions for the additional items that you want to include in the proxy. 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.
    /enum:EnumName

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

    /class:ClassName
    /method:ClassName.MethodName

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

    /table:TableName
    /method:TableName.MethodName


  • When you are finished making changes, close the Web Application Editor and be sure to save the changes.

Step 2

Deploy the updated proxy


  • In Ax, click Microsoft Dynamics AX > Tools > Development Tools > Web development > Proxies.
  • 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.

Step 3

Reference proxy

  • Open the Visual Studio project for your User Control.
  • In Solution Explorer, right-click the App_Code group. Click Generate Proxies to create the C# files that provide the interface for the proxy.
  • 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;

Step 4

Use items added 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
{
get
{
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 AddNumber method from the Test class.

protected void Button_AddNo(object sender, EventArgs e)
{
string test_result;
using (IAxaptaAdapter a = this.AxSession.AxaptaAdapter)
{
test_result = Test.AddNumber(a, Convert.ToInt16(No1.Text), Convert.ToInt16(No2.Text));
Total.Text = test_result;
}
}

Popular posts from this blog

What does this mean: "The form datasource query object does not support changing its AllowCrossCompany property after the form has executed the query."?

I have made a form with datasources vendtable and vendtrans. Inside vendtable_ds.executequery() looks like this: QueryBuildDataSource queryBuildDatasource ,queryBDS_VendTrans_Invoice; ; queryBuildDatasource = this.query().dataSourceTable(tablenum(vendtable)); queryBDS_VendTrans_Invoice = this.query().dataSourceTable(tablenum(vendtrans)); if (curext() == "MASTERCOMP") { this.query().allowCrossCompany(true); } else { this.query().allowCrossCompany(false); } //FilterVendorName = stringedit control on form if (FilterVendorName.text()) { queryBuildDatasource.addRange(fieldNum(VendTable,Name)).value(strfmt("*%1*", FilterVendorName.text())); } else { queryBuildDatasource.clearRange(fieldNum(VendTable,Name)); } //FilterInvoiceNumber = stringedit control on form if (FilterInvoiceNumber.valueStr() == "") { queryBDS_VendTrans_Invoice.enabled(false); } else { queryBDS_VendTrans_Invoice.enabled(true); queryBDS_VendTrans_In...

Credit Note [Dynamics AX] using X++

This post will help to create credit note for a sales order based on the invent lot id. All the invoices raised for a particular sales line – Lot Id will be raised back as a credit note. Information on Credit Note: A credit note or credit memorandum (memo) is a commercial document issued by a seller to a buyer. The seller usually issues a Credit Memo for the same or lower amount than the invoice, and then repays the money to the buyer or sets it off against a balance due from other transactions Below Code will help to create credit note for all the invoices raised against the sales line -lot id. Please note: This code can be customized as per your requirements. This is just a template to help creating credit note using X++ code. Please test the code before use. static void SR_CreateCreditNote_Sales(Args _args) { // Coded by Sreenath Reddy CustInvoiceTrans custInvoiceTrans; Dialog dialog = new Dialog(“Create credit note – for sales.”); DialogField dfInv...