M-Files UI Extensibility Applications can contain .Net Assemblies. The JavaScript code can instantiate an object from the class implemented in the assembly, and delegate tasks for the .Net implementation. This is a guide for implementing, delivering and using .NET assembly as a part of UI Extensibility application package.
The technique descriped in this article is available in M-Files 10.1.3915.45 and newer. Note that .NET based technology will be available on native Windows clients only.
Implementation
Create a class assembly project in Visual Studio. The project should target .NET Framework version 4 or newer. Implement a public class that exposes callable methods or other members. These methods, properties or fields can be used from JavaScript code.
It the interface does not need to be COM-visible, but some limitations apply because of the diffirences in the execution environment. In general basic types can be used without trouble. Complex types passed from .NET to JavaScript show as JavaScript objects in JavaScript, and the members can be used with their names. Any complex type from JavaScript to .NET needs to be typed as dynamic type in .NET, because the JavaScript objects are loosely typed. The dynamic type enables late-binding (run-time binding) in .NET assembly when resolving the type members.
Also M-Files API objects must be treated as dynamic types in .NET. The .NET assembly must not include M-Files API library reference, or use strictly-typed M-Files API types. This is because each M-Files client version has its own identities for its objects so that they do not disturb each others.
A simple example implementation of a .NET assembly code:
UI Ext. callable class in C# project |
Copy Code
|
---|---|
using System; using System.Windows.Forms; namespace ClassLibrary1 { public class Class1 { public void ShowMessage( Int32 parentHWND, string message, dynamic vault ) { IWin32Window parentWindow = Control.FromHandle( ( IntPtr )parentHWND ); MessageBox.Show( parentWindow, vault.Name ); } } } |
Deployment
Compile the project and place the output .dll in to the UI Ext. application package file (the .zip file or .mfappx file). To enable debugging add the .pdb file next to the .dll file. These files can go to the application root folder where the appdef.xml file is, or a subfolder. When the application is installed to the M-Files vault and it gets distributed to M-Files clients, the embedded assembly follows in the application package.
Using the .NET Assembly
The .NET object can be instantiated from JavaScript with CreateObjectCLR Method. The .NET object stays present at least as long as the reference to the object is preserved.
The .NET object lives in the same process where the calling code runs. E.g. if the UI Extensibility application runs in Windows Explorer, the explorer.exe process runs the .NET object, too. Therefore the .NET code can be debugged by attaching the debugger to the explorer.exe.
Creating and using .NET object from JavaScript |
Copy Code
|
---|---|
var o = MFiles.CreateObjectCLR( "ClassLibrary1.dll", "ClassLibrary1.Class1" ); o.ShowMessage( shellFrame.OuterWindow.Handle, "Hello!", shellFrame.ShellUI.Vault ) |