MFiles Global Object
The MFiles global object is available in your UI Extension code after initialization. It provides access to enumerations, utility functions, and the Vault API enums.
The MFiles global object is only available after the UI Extension has been initialized. It is automatically injected into your extension's iframe context.
Overview
The MFiles global object contains:
| Namespace/Property | Description |
|---|---|
MFiles.Event | UI Extension event type constants |
MFiles.MenuLocation | Menu location constants for custom commands |
MFiles.CommandState | Command state constants (active, inactive, hidden) |
MFiles.CommandLocation | Command location constants |
MFiles.BuiltinCommand | Built-in M-Files command identifiers |
MFiles.VaultEnums | gRPC/Vault API enumerations |
MFiles.* (ICommonFunctions) | Utility methods from ICommonFunctions |
UI Extension Enumerations
These enumerations are used when working with UI Extension features like events, commands, and menus.
MFiles.Event
Event type constants used when registering event handlers.
// Register a handler for the Started event
shellUI.Events.Register(MFiles.Event.Started, async () => {
console.log("ShellUI started");
});
// Register a handler for NewShellFrame event
shellUI.Events.Register(MFiles.Event.NewShellFrame, async (shellFrame) => {
console.log("New shell frame created");
});
// Register a handler for custom commands
shellFrame.Commands.Events.Register(MFiles.Event.CustomCommand, async (commandId) => {
console.log("Custom command executed:", commandId);
});
See: Event Enumeration
MFiles.MenuLocation
Constants for specifying where custom commands appear in menus.
// Add command to the top pane menu
await shellFrame.Commands.AddCustomCommandToMenu(
commandId,
MFiles.MenuLocation.MenuLocation_TopPaneMenu,
0
);
MFiles.CommandState
Constants for setting the state of custom commands.
// Set command as active (enabled)
await shellFrame.Commands.SetCommandState(
commandId,
MFiles.CommandLocation.MainMenu,
MFiles.CommandState.CommandState_Active
);
// Hide a command
await shellFrame.Commands.SetCommandState(
commandId,
MFiles.CommandLocation.MainMenu,
MFiles.CommandState.CommandState_Hidden
);
MFiles.CommandLocation
Constants for specifying command locations.
await shellFrame.Commands.SetCommandState(
commandId,
MFiles.CommandLocation.MainMenu,
MFiles.CommandState.CommandState_Active
);
See: CommandLocation Enumeration
MFiles.BuiltinCommand
Identifiers for built-in M-Files commands.
// Execute the built-in "New Object" command
await shellFrame.Commands.ExecuteCommand(MFiles.BuiltinCommand.NewObject, null);
See: BuiltinCommand Enumeration
Vault API Enumerations (MFiles.VaultEnums)
The MFiles.VaultEnums namespace contains enumerations used when working with the Vault gRPC API. These are essential when constructing search conditions, working with property values, and interacting with vault data.
Common VaultEnums
These are some of the most commonly used enumerations. For the complete list, see gRPC Enums Reference.
| Enumeration | Purpose |
|---|---|
MFiles.VaultEnums.ConditionType | Search condition types (equal, contains, etc.) |
MFiles.VaultEnums.Datatype | Property data types |
MFiles.VaultEnums.ExpressionType | Search expression types |
MFiles.VaultEnums.DataFunction | Data functions for search conditions |
MFiles.VaultEnums.FolderType | Folder type constants |
MFiles.VaultEnums.StatusType | Status type constants |
MFiles.VaultEnums.ObjectVersionDataRequestVersionType | Version request types |
Example: Building a Search Condition
// Search for documents containing "invoice" in any field
const searchCondition = {
expression: {
type: MFiles.VaultEnums.ExpressionType.EXPRESSION_TYPE_ANY_FIELD
},
type: MFiles.VaultEnums.ConditionType.CONDITION_TYPE_CONTAINS,
typedValue: {
value: "invoice",
datatype: {
type: MFiles.VaultEnums.Datatype.DATATYPE_TEXT
}
}
};
Example: Creating a Search Folder
const searchFolder = {
type: MFiles.VaultEnums.FolderType.FOLDER_TYPE_SEARCH_FOLDER,
searchDef: {
conditions: [
{
expression: {
propertyValue: {
property_def: 100,
data_function: MFiles.VaultEnums.DataFunction.DATA_FUNCTION_NO_OP
},
type: MFiles.VaultEnums.ExpressionType.EXPRESSION_TYPE_PROPERTY_VALUE
},
type: MFiles.VaultEnums.ConditionType.CONDITION_TYPE_EQUAL,
typedValue: {
value: true,
datatype: {
type: MFiles.VaultEnums.Datatype.DATATYPE_BOOLEAN
}
}
}
]
}
};
ICommonFunctions Methods
The MFiles global object also includes methods from ICommonFunctions. These provide utility functions that can be called directly on the MFiles object.
Web Storage
// Save data to web storage
await MFiles.WriteToWebStorage("myKey", "myValue");
// Read data from web storage
const value = await MFiles.ReadFromWebStorage("myKey");
UI Utilities
// Get the current accent color
const accentColor = await MFiles.GetAccentColor(); // returns "#006eef"
// Show a message box
await MFiles.ShowMessage("Hello World!");
// Report an exception
await MFiles.ReportException(error);
Vault Access
// Get current vault GUID
const vaultGuid = await MFiles.GetVaultGuid();
Complete Example
Here's a complete example showing various MFiles global object usage:
// main.js - Entry point of a UI Extension
async function OnNewShellUI(shellUI) {
// Register for the Started event
shellUI.Events.Register(MFiles.Event.Started, async () => {
console.log("ShellUI started");
// Read a stored preference
const lastView = await MFiles.ReadFromWebStorage("lastView");
console.log("Last view:", lastView);
});
// Register for new shell frames
shellUI.Events.Register(MFiles.Event.NewShellFrame, async (shellFrame) => {
await OnNewShellFrame(shellFrame);
});
}
async function OnNewShellFrame(shellFrame) {
// Wait for the shell frame to start
shellFrame.Events.Register(MFiles.Event.Started, async () => {
// Create a custom command
const commandId = await shellFrame.Commands.CreateCustomCommand("My Command");
// Add to menu
await shellFrame.Commands.AddCustomCommandToMenu(
commandId,
MFiles.MenuLocation.MenuLocation_TopPaneMenu,
0
);
// Set command as active
await shellFrame.Commands.SetCommandState(
commandId,
MFiles.CommandLocation.MainMenu,
MFiles.CommandState.CommandState_Active
);
// Handle command execution
shellFrame.Commands.Events.Register(MFiles.Event.CustomCommand, async (id) => {
if (id === commandId) {
await MFiles.ShowMessage("Custom command executed!");
}
});
});
}
See Also
- Testing and Developing - Debugging tips and runtime environment info