Thursday, October 23, 2014

C# Accessing AX Session Information & Defining which AOS to connect to via code

So while working on the last post I ran into an issue where I couldn't change which aos the data was being pulled in from so I wrote the following function that gets server information so you can see basic information on what connection is being used. Along with defining which AOS the code pulls from by defining a server within code so I can switch back and forth between DEV & TEST

You can download the C# visual studio project here: download now!


Its good to note that around the web the following "logon" statement is posted

// Logon to Dynamics AX
Session axSession = new Session();
axSession.Logon(null, null, null, null);

However in order to define which AOS server to connect to via code we need to do the following

// Logon to Dynamics AX
Session axSession = new Session();
axSession.Logon(null, null, "AOSServerName", null);




The following code are just some ways to pull in basic server information


private void GetAOSInfo_Click(object sender, EventArgs e)
{
 
try
{
 
// Logon to Dynamics AX
Session axSession = new Session();
axSession.Logon(null, null, axServerName, null);


 
//get server information

string axInfoAOSInstance = axSession.CreateObject("XSession").Call("AOSName").ToString();
string axInfoClientName = axSession.CreateObject("XSession").Call("clientComputerName").ToString();
string axInfoLoginDateTime = axSession.CreateObject("XSession").Call("loginDateTime").ToString();
string axInfoMasterSessionId = axSession.CreateObject("XSession").Call("sessionId").ToString();
string axInfoUserId = axSession.CreateObject("XSession").Call("userId").ToString();



CustomerOutput.Clear();
 
CustomerOutput.AppendText(("AOS Server: " + axInfoAOSInstance));
CustomerOutput.AppendText((Environment.NewLine + "Client Computer Name: " + axInfoClientName));
CustomerOutput.AppendText((Environment.NewLine + "Session Id: " + axInfoMasterSessionId));
CustomerOutput.AppendText((Environment.NewLine + "Login DateTime: " + axInfoLoginDateTime));
CustomerOutput.AppendText((Environment.NewLine + "User Id: " + axInfoUserId));



 
 
//log off ax
axSession.Logoff();

}
 
catch (Exception ex)
{

CustomerOutput.Clear();

CustomerOutput.AppendText(ex.Message.ToString());

}

}
 

Accessing AX CustTable & DirPatyTable (Customer Info) via C# via Linq.

In order to support legacy applications sometimes you need to pull in data from AX into standalone applications. Below will show you how to pull in customer id's + names (CustTable + DirPartyTable)
The following examples will show you how to manually loop through that data or tie it to a combo box via Linq.

You can download the C# Visual Studio Project Here: download now!

Step 1. Start new project within visual studio that has access to an aos via the application explorer
Step 2. Adding the following references to the visual studio project

C:\Program Files (x86)\Microsoft Dynamics AX\6.0\Client\Bin\

Microsoft.Dynamics.AX.Framework.Linq.Data.dll
Microsoft.Dynamics.AX.Framework.Linq.Data.Interface.dll
Microsoft.Dynamics.AX.Framework.Linq.Data.ManagedInteropLayer.dll
Microsoft.Dynamics.AX.ManagedInterop.dll


Then on your form/class add the following using classes

using Microsoft.Dynamics.AX.ManagedInterop;
using Microsoft.Dynamics.AX.Framework.Linq.Data;
using System.Linq;

Step 3. Right click on project and add to AOT
Step 4. After step 3 you can start to add objects from the application explorer into the project. Add this time we should add the table CustTable & DirPartyTable

The following code block will show you how to connect to ax, query the tables and loop through the data or tie it to multiple combo boxes. Its good to note that my name space for this project was Win2AX.

       


 
 


public void loadCustomers(Boolean listInTextBox)
{
           

try
{
               

isCustomersLoaded = false;

// Logon to Dynamics AX (usering windows logon info)
Session axSession = new Session();
axSession.Logon(null, null, axServerName, null);

// Create a query provider needed by the Linq Provider
QueryProvider provider = new AXQueryProvider(null);

//Connect to the table proxy's for the CustTable and DirPartTable tables within AX.
QueryCollection<Win2AX.CustTable> customerCollection = new QueryCollection<Win2AX.CustTable>(provider);

QueryCollection<Win2AX.DirPartyTable> partyCollection = new QueryCollection<Win2AX.DirPartyTable>(provider);



 
//select the account num and dirparty name so we can provide an account num + name

//its good to note that when I didnt include a field list the app crashed. I think linq is limited to the amount of data that can be processed

var allCustomers = (from customer in customerCollection
join partyDesc in partyCollection on customer.Party equals partyDesc.RecId
orderby customer.AccountNum ascending
select new { customer.AccountNum, partyDesc.Name, Description = customer.AccountNum + " - " + partyDesc.Name }).ToList();

allCustomers.Insert(0, new { AccountNum = "<Select Customer>", Name = "<Select Customer>", Description = "<Select Customer>" });

if (listInTextBox)
{

CustomerOutput.Clear();
//go through all of the records

foreach (var custTable in allCustomers)
{
//output the current record
CustomerOutput.AppendText(Environment.NewLine + "Customer Account: " + custTable.AccountNum + " Description: " + custTable.Name);
}

}
else if (!listInTextBox)
{
//bind all combo boxes to our linq list

//combination (Customer Id - Name)
CustomerList.DataSource = allCustomers;
CustomerList.DisplayMember = "Description";
CustomerList.ValueMember = "AccountNum";

//Customer Id only
CustomerIdSelection.DataSource = allCustomers;
CustomerIdSelection.DisplayMember = "AccountNum";
CustomerIdSelection.ValueMember = "AccountNum";

//Name Selection / Account Num Value
CustomerNameSelection.DataSource = allCustomers;
CustomerNameSelection.DisplayMember = "Name";
CustomerNameSelection.ValueMember = "AccountNum";

//signal that all combo boxes are loaded so we can handle comboboxes selected index changes correctly

isCustomersLoaded = true;
}

 
//log off ax
axSession.Logoff();

}
catch (Exception ex)
{

CustomerOutput.Clear();
CustomerOutput.AppendText(ex.Message.ToString());

}

Friday, October 10, 2014

Failed to create a session; confirm that the user has the proper privileges to log on to Microsoft Dynamics

Currently we have a developer working/coding in an active environment where users are currently using the system (non-prod) and had an issue generating good incremental CIL. During this time some of the users started to see the following message

"Failed to create a session; confirm that the user has the proper privileges to log on to Microsoft Dynamics"

In order to resolve this issue try the following

File/Tools/Options/Development/ uncheck the option for "Execute business operations in CIL"

This ended up fixing our issue, but after reading multiple other blogs about AX if this doesn't work then the next step is to run a full CIL. If that doesn't work you should restart the AOS and run the full CIL again.