How to add .net based user control to AX 2009

if you would like to add any user control prepared on .net platform into AX 2009 you should follow the steps:
1-Create Active X and wrap the user control with unmanaged code.
2-register to windows as COM object.
3-call it from AX.

1- Here is sample code:
//create Class type project in Visual Studio

//Set events that will be used in AX
//presents to caller
[ComVisible(true)]
//create interface type of Dispatch
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
//Create unique key, used by windows
//you can create a guid from tools menu on visual studio easily.
[Guid("102B961D-60B6-4990-8B29-4802F7F9D293")]

public interface IEventInterface
{
[DispId(1)]
//define events that we want to use
void ValidateEdit(object sender, ValidateEditEventArgs e);
}
[ComVisible(true)]
//class unique ID
[ProgId("DC.ActiveX.C1FlexGrid")]
//declera EventInterface class for the class
[ComSourceInterfaces(typeof(IEventInterface))]
//class type should be AutoDual
[ClassInterface(ClassInterfaceType.AutoDual)]
[Guid("3F93920F-08B7-40cf-A327-AE9206257CB5")]
public class DC_FlexGridC1Class : UserControl with full namespace (ex:C1.Win.C1FlexGrid.C1FlexGrid -> this user control must have every events or methods used on this class project)
{
....
public DC_FlexGridC1Class()
{
}

[ComVisible(true)]
public void InitializeActiveX()
{
conn = new System.Data.SqlClient.SqlConnection();
cmd = new System.Data.SqlClient.SqlCommand();
ds = new System.Data.DataSet();
adapter = new System.Data.SqlClient.SqlDataAdapter();
}

//to register by regasm command on .net framework
[ComRegisterFunction()]
public static void RegisterClass(string key)
{
// Strip off HKEY_CLASSES_ROOT\ from the passed key as I don't need it
StringBuilder sb = new StringBuilder(key);
sb.Replace(@"HKEY_CLASSES_ROOT\", "");
// Open the CLSID\{guid} key for write access
RegistryKey k = Registry.ClassesRoot.OpenSubKey(sb.ToString(), true);
// And create the 'Control' key - this allows it to show up in
// the ActiveX control container
RegistryKey ctrl = k.CreateSubKey("Control");
ctrl.Close();
// Next create the CodeBase entry - needed if not string named and GACced.
RegistryKey inprocServer32 = k.OpenSubKey("InprocServer32", true);
inprocServer32.SetValue("CodeBase", Assembly.GetExecutingAssembly().CodeBase);
inprocServer32.Close();
// Finally close the main key
k.Close();
}


///
/// Called to unregister the control
///

/// Tke registry key

//to unregister by regasm command on .net framework
[ComUnregisterFunction()]
public static void UnregisterClass(string key)
{
StringBuilder sb = new StringBuilder(key);
sb.Replace(@"HKEY_CLASSES_ROOT\", "");
// Open HKCR\CLSID\{guid} for write access
RegistryKey k = Registry.ClassesRoot.OpenSubKey(sb.ToString(), true);
// Delete the 'Control' key, but don't throw an exception if it does not exist
k.DeleteSubKey("Control", false);
// Next open up InprocServer32
RegistryKey inprocServer32 = k.OpenSubKey("InprocServer32", true);
// And delete the CodeBase key, again not throwing if missing
k.DeleteSubKey("CodeBase", false);
// Finally close the main key
k.Close();
}
}

2-
to register as COM Object;
write command prompt with initialize parameter ->
regasm "C:\Documents and Settings\Administrator\Desktop\ActiveX.dll"
/tlb:"C:\Documents and Settings\Administrator\Desktop\ActiveX.tlb" /codebase
(it can be found C:\Program Files\Microsoft Visual Studio 9.0\VC> )

to unregister;
regasm /u "C:\Documents and Settings\Administrator\Desktop\ActiveX.dll"
3-
After register COM object. This object can be accessible by AX.
-Create new Form
-Right click on design and select "new control > ActiveX"
-find out newly created ActiveX
-click OK...(bingo it is added:) )
-if you right click over newly added activeX you will see ActiveX Explorer.you can manage defined Events and Methods

do not forget adding isolated code block in AX;

InteropPermission perm = new InteropPermission(InteropKind::ComInterop);

....
perm.assert();
.
.
.
COM objects part
.
.
.

CodeAccessPermission::revertAssert();

....

good luck...

Comments

Popular posts from this blog

Complex Query in QueryExpression in Microsoft CRM 2011

Exception caught instantiating TERADATA report server extension SQL Reporting Services

Microsoft Power Apps Portal integration with Dynamics 365 CE On-Premise - Step By Step Guide