Monday, February 21, 2011

.NET AX business connector

Integrate an Application with Microsoft Dynamics AX Using .NET Business Connector

The Microsoft Dynamics AX development environment enables you to integrate a .NET-connected application with Microsoft Dynamics AX. This feature gives a simplified way to develop integrations with Microsoft Dynamics AX using standard Microsoft developer tools such as Visual Studio .NET and the .NET Framework.

List of classes provided by business connector
• Axapta - The Axapta class provides the ability to connect to the Axapta system, create Axapta classes, create Axapta record, container, and buffer objects, execute transactions, and perform other Axapta system tasks.
• AxaptaBuffer - The AxaptaBuffer class provides the ability add data to and retrieve data from an Axapta buffer.
• AxaptaContainer - The AxaptaContainer class provides the ability to read and write to and from Axapta containers.
• AxaptaObject - The AxaptaObject class provides the ability to call Axapta class methods.
• AxaptaRecord - The AxaptaRecord class provides functionality for reading and modifying Axapta records.

Prerequisites
________________________________________
You will need to have:
• Microsoft Dynamics AX
• Visual Studio 2005 or Visual Studio 2008
• 2.0 .NET Framework
This walkthrough illustrates the following tasks:
• Create a C# Windows project
• Add a reference to .NET Business Connector
• Create the layout of a form
• Add code behind the form


Creating a C# Windows Project
________________________________________
The first step is to create a Windows application that will access data in Microsoft Dynamics AX.
To create a C# Windows project
1. Open Visual Studio.
2. From the File menu, click New, and then click Project to display the New Project dialog box.
3. In the Project types pane, click Visual C#. In the Templates pane, click Windows Application.
4. Set the name to DisplayAddressState.
5. To change the location of the solution directory, click Browse, and specify a new location.
6. Click OK.
Adding a Reference to .NET Business Connector
________________________________________
To access Microsoft Dynamics AX data, you must create a connection using .NET Business Connector. In this section, you will add a reference to the .NET Business Connector assembly.
To add a reference to .NET Business Connector
1. In Solution Explorer, right-click References, and then click Add Reference.
2. In the Add Reference window, click the Browse tab.
3. Specify the location of Microsoft.Dynamics.BusinessConnectorNet.dll, and then click Add.
4. Click OK.
Creating the Layout of a Form
________________________________________
After you have created DisplayAddressState, you can add controls to the form by dragging them from the toolbox.
To create the layout a form
1. In the toolbox, click the Label control, and then add it to the form.
2. Click the TextBox control and add it to the form next to the Label.
3. Click Button and add it to the form next to the TextBox.
4. Click Button and add it to the form next to the previous button.

Adding Code Behind the Form
________________________________________
In this section, you will add code behind the form to extract the data from Microsoft Dynamics AX. You will use the AxaptaRecord class to execute a query. This query will extract data from Microsoft Dynamics AX. You will retrieve and display customer data on the controls that you have added to the Windows form.
To add code on code behind of the form
On the form, double-click button1 to access the DisplayAxCustomer.Form1 code.
To fetch data from Axapta table:
using Microsoft.Dynamics.BusinessConnectorNet;
private void button1_Click(object sender, EventArgs e)
{
Axapta DynAx = new Axapta();
AxaptaRecord DynRec;

// Authenticate the user and establish a session.
DynAx.Logon(null, null, null, null);

// Define the record as the AddressState table.
DynRec = DynAx.CreateAxaptaRecord("AddressState");

// Define the query that will run on the AddressState records.
// This will return all the data in the AddressState table with
// the cursor positioned at the first record in the table.
DynRec.ExecuteStmt("select * from %1");

// Check if the query returned any data.
if(DynRec.Found)
{
// Display the record on the form that was retrieved from the query.
textBox1.Text = (string) DynRec.get_Field("Name");
}
}

Inserting records to Axapta table:

private void button2_Click(object sender, EventArgs e)
{
// Create the .NET Business Connector objects.
Axapta ax;
AxaptaRecord axRecord;
string tableName = "AddressState";

try
{
// Login to Microsoft Dynamics AX.
ax = new Axapta();
ax.Logon(null, null, null, null);

// Create a new AddressState table record.
using (axRecord = ax.CreateAxaptaRecord(tableName));
{

// Provide values for each of the AddressState record fields.
axRecord.set_Field("NAME", "MyState");
axRecord.set_Field("STATEID", "MyState");
axRecord.set_Field("COUNTRYREGIONID", "US");
axRecord.set_Field("INTRASTATCODE", "");

// Commit the record to the database.
axRecord.Insert();
}
}
catch (Exception e)
{
Console.WriteLine("Error encountered: {0}", e.Message);
// Take other error action as needed.
}