Creating Dynamics AX user controls for Enterprise portal.
Dynamics AX user controls are ASP.net user controls developed in Visual studio 2008, parked in Dynamics AX application and can be used in Enterprise portal.
In section 1, I explained about how to create a simple user controls and in section 2, I explained about how to connect the web parts and in section 3, I explained about how to call X++ methods within the web part.
Create a Dynamics web project in visual studio:
Open visual studio 2008.
File ->New ->Web site.
In new website selection screen select the template ‘Dynamics AX web project’ under ‘My templates’ section.
Select Location as HTTP and provide the project name
Language: Visual C#
A new solution will be created with a project.
Right click the solution project and select ‘Add New Item’.
Select the ‘Dynamics AX User control’ as template which is under ‘My templates’.
Provide the name of the control.(SampleUserControl)
Select the language as the C#. Click OK.
New control “SampleUserControl” will be added to project.
Add the control to Dynamics AX AOT :
Right click the control and select “Add to AOT’. The user control will be added to AOT under AOT/Web/Webfiles/Web Controls.
Also a managed content item will be created under AOT/Web/Web Content/Managed which is pointing to user control SampleUserControl. This managed content item will be visible in enterprise portal.
Create a dataset in AX
Data set acts as a data source for the user control. Data sets can be created in AOT.
Under AOT/data sets, Create a new data set and name it as CustomerSample.
Data sets look exactly like the AX forms without design node.
For the CutomerSample data set add CustTable as the data source.
Save the dataset.
Design the control in visual studio:
Now go to visual studio and right click the sampleUserControl and click view designer.
Design mode of the control will be opened.
Now open the tool box and you find the Dynamics AX group in the tool box.(If it is not available then right click on tool bar and choose items they are available in .Net Framework Components tab)
Add the AXDataSource from the tool box.
Rename the data source to SampleCustDS.
Select the SampleCustDS in design mode and click > mark at the right top corner.
It asks for Dataset name. Select the ‘CustomerSample’ that you have created in AOT.
Now add AX grid view.
Select the AX grid view and open the properties screen and select the datasource as SampleCustDS and then > mark and then Edit columns option.
Under available fields from bound fields add AccountNum,Name, InvoiceAccount,CustGroup, Currency to selected fields.
Now right click the SampleUserControl and select ‘Refresh AX user control’. This will refresh the changes to AOT.
Creating a web part page and adding user control to it:
Now open Enterprise portal and select site actions and then create.
Select web part page under web pages.
Give the name of the page as “CustomerSample” and then choose the layout.
Select the document library as “Enterprise portal”.
A new blank page will be created.
Here we need to add the required web parts.
Under the body section click “add a web part”.
From the dialog opened select the “Dynamics User control web part” to hold the user control that we have just created and then click ‘Add’.
A blank Dynamics User control web part will be added. Now go to Edit at the right corner of the web part and click the arrow mark and then modify shared web part from the context menu.
Dynamics AX web part properties editing window will be opened to the right of the page.
Under the managed content item select the “SampleUserControl” managed content item. Click Apply and then OK.
Exit the edit mode by clicking exit edit mode below the site actions.
This will display the data from the Custtable in the design that you have created in visual studio.
Go to visual studio 2008.
Add a new control named “CustomerContacts” to the same project that you have created above. Go to design part and add the AXDataSource’ control and rename to ‘CustDS’ and point to same dataset “CustomerSample”.
Add the AX form control and Select the datasource as ‘CustDS’ that you have created just now by clicking on > at the top right of form control. Select the datamember property(CustTable_current) also from properties window in visual studio.
Add an AXGroup inside AX form from tool box.
Select the AX group and go to properties window. For the fields property click on … symbol. A new window opens.
Select the required fields like Address, Phone, TeleFax, URL, Email etc,.
Add the user control to AOT.
Now go to the same EP page that you have created in section I above and add the new user control web part and point to the “CustomerContacts” managed content item.
Connecting two web parts:
By selecting the modify shared web part option, open the Dynamics properties window for first web part (SampleUserControl) and make the web part role as provider.
Open the Dynamics properties window for second web part (CustomerContacts) and make the role as Consumer to make this web part the consumer of information.
Now apply the changes by clicking on Apply.
Now on the first web part click on Edit and then connections àSend AX contextList to and then select the second web part.
You can verify it from the second web part; here it shows as ‘Connections àGet AXContextList from àfirst web part name.
Now if you change the section in the first web part that is if the customer selection is changed in grid the contact details for customer will automatically be changed in the second web part.
In the first two examples we have written user controls without writing a single line of C# code. And we did not call X++ methods explicitely. But, sometimes it may be required to call the X++ class methods or table methods to get the related information. For example if the customer type is organization then we may need to get the number of employees, organization number of the customer (These details can be seen on Details tab of CustTable form in rich client.) Which are being stored in table DirOrganizationDetail. In the same way if the customer type is person we may need BirthDate and MarritalStatus which are stored in DirPersonPartyDetail. This we can achieve by writing a code in C# to call X++ methods.
I will explain how to get these details by calling table methods.
Import the below namespaces before adding the following lines of code.
For the above control CustomerContacts add the field lblNumberOfEmployees. This will display the number of employees in the selected customer organization.
In the pade_load () of CustomerContacts write the following line if code.
And write the event hander for ‘CustomerContacts_ExternalContextChanged’ like below
void CustomerContacts_ExternalContextChanged(object sender, Microsoft.Dynamics.Framework.Portal.UI.AxExternalContextChangedEventArgs e)
IAxaptaRecordAdapter currentRecord = AxBaseWebPart.GetWebpart(this).ExternalRecord;
ISession currentSession = AxBaseWebPart.GetWebpart(this).Session;
IAxaptaAdapter axAdapter = currentSession.AxaptaAdapter;
obj1 = axAdapter.CallStaticRecordMethod(“CustTable”, “find”, currentRecord.GetField(“AccountNum”).ToString());
recordAdapter = axAdapter.CreateAxaptaRecord(obj1);
partyId = recordAdapter.GetField(“partyId”).ToString();
recordAdapter = axAdapter.CreateAxaptaRecord(obj2);
lblNumberOfEmployees.Text = recordAdapter.GetField(“NumberOfEmployees”).ToString();
Here first I get the customer record by passing the customer id that was selected in the grid.
And then get the party id of the customer from the customer record. Pass the party id to find method of DirOrganizationDetail to get the DirOrganizationDetail record.
From DirOrganizationDetail we can get number of employees directly.