The vSphere HTML Client SDK offers a set of JavaScript APIs which facilitate the building of HTML Client-only plugins, fully compatible with the HTML-based vSphere Client.
These JavaScript APIs are documented here as if they have TypeScript signatures, but they run as pure JavaScript, and all complex types are plain old Javascript objects.
The JavaScript APIs may be used by the plugin to communicate with the HTML Client and to execute a number of operations. Each plugin will have access to the JavaScript APIs within its own iframe which eliminates the possibility of plugin conflicts. Please refer to the Plug-in Architecture Diagram in the online vSphere Client SDK documentation.
WARNING: Do not access the window.parent(the Client window), as this is unsupported and if your plugin relies on this it might break in a future release of the vSphere Client.
The JavaScript APIs are organized in several domains depending on their functionality. This improves readability and communicates clearly the API functionality scope:
The JavaScript APIs can be accessed by getting the API object directly from the extension's iframe, such as:
window.frameElement.htmlClientSdk
Note: For practical examples on how to use the JS APIs for local plugins, see the "html-sample" plugin. In order to make the following examples more readable, we will use a shortcut for the JS API object, namely
"htmlSdk" which will be equal to "window.frameElement.htmlClientSdk".
Remote plugins keep for the most part the frontend APIs that were shipped in the previous release.
In order to isolate plugins a new asynchronous way for communicating with the parent window was introduced, namely the plugin communicates with the vSphere Client with a window.postMessage mechanism.
Client Library was introduced to abstract plugin writers from this new mechanism. The Client Library exposes the Frontend APIs in a standard manner, and hides the complexity associated with asynchronious programming.
The Client Library is not bundled with plugins and is dynamically loaded through a well known location. This approach allows for transparent bug fixes and new APIs to be delivered in future vSphere Client releases.
WARNING: Do not reverse engineer the Client Library and do not use anything not provided by the APIs. Doing this might break your plugin in future vSphere Client releases.
The JavaScript APIs can be accessed by including the "/api/ui/htmlClientSdk.js" source. For example: <script src="/api/ui/htmlClientSdk.js"/>.
Once the htmlClientSdk.js source is included, the htmlClientSdk object is created in the iframe's window which has two functions:
The modal APIs are used to perform various actions for a modal dialog such as open, close and configure. The modal APIs can be accessed with the htmlSdk.modal.{API}
| API | Description | Example usage |
|---|---|---|
| open(configObj: ModalConfig) => void | Opens a modal dialog and accepts for parameter a configuration object, which contains:
interface ModalConfig { url: string; title?: string; size?: { width: number, height: number }; closable?: boolean; onClosed?: (result?: any) => void; contextObjects?: any[]; customData?: any; } Where:
|
var config = { url: "resources/editDatacenter.html", title: "Edit Datacenter", size: { width: 490, height: 240 }, onClosed: function(id) { alert("Modal " + id + " closed."); }, contextObjects: [{ id: "urn:vmomi:Datacenter:datacenter-1" }, { id: "urn:vmomi:Datacenter:datacenter-2" }], customData: {idsRange: [1, 9]} }; htmlSdk.modal.open(config); |
| close(data?: any) => void | Closes the already opened modal. The modal is the one corresponding to the iframe from which this API was called from.
If data has been provided it will be passed to the onClose callback function if such was specified in the parameter of the modal.open API. |
var modalId = 2;
htmlSdk.modal.close(modalId); |
| setOptions(configObj: DynamicModalConfig) => void | Changes the configuration of the modal with properties specified in the parameter object.
The configObj type is defined as follows: interface DynamicModalConfig { title?: string; height?: number; } Where:
|
htmlSdk.modal.setOptions({
title: "New Title", height: 450 }); |
| getCustomData() => any | Returns the custom data that was provided upon opening the modal through customData property or null if the customData property was not set. |
htmlSdk.modal.getCustomData();
for example: {idsRange: [1, 9]} |
The client application APIs are related to the HTML Client application. The client application APIs can be accessed with the
htmlSdk.app.{API}
| APIs | Description | Example usage |
|---|---|---|
| getContextObjects() => any[] |
Returns current context objects:
|
htmlSdk.app.getContextObjects();
for example: [ { id: "urn:vmomi:Datacenter:datacenter-1" } ] |
| navigateTo(configObj: NavigationOptions) => void | Navigates to a specified view with an optional custom data which will be passed to the view.
The configuration object should contain the following information: interface NavigationOptions { targetViewId?: string; objectId?: string; customData?: any; } Where:
When navigating to a global view, the "objectId" property can be skipped. Note: For Remote plugins the navigateTo API can be called with the targetViewId parameter only if the view is defined by the plugin itself, otherwise only the objectId parameter can be used to navigate to a specific object. This means that the the navigation to a cluster monitor view will not work if the view is not defined in the plugin.json of the plugin. |
htmlSdk.app.navigateTo({
targetViewId: "acme-plugin-monitor-view", objectId: "urn:vmomi:Datacenter:datacenter-1", customData: {name: "test", filter: true} }); |
| getNavigationData() => any | Retrieves the navigation data passed to the current view by the navigateTo() API.
|
If the following is called: htmlSdk.app.navigateTo({ targetViewId: "acme-plugin-monitor-view", objectId: "urn:vmomi:Datacenter:datacenter-1", customData: {name: "test", filter: true} }); Then, any call to: htmlSdk.app.getNavigationData(); will return: {name: "test", filter: true} |
| getClientInfo() => ClientInfo | Returns information about the current vSphere Client, such as "version", "type" etc.
interface ClientInfo { type?: string; version?: string; } Where:
|
htmlSdk.app.getClientInfo();
for example: {type: "HTML", version: "6.5.1"} |
| getClientLocale() => string | Returns the current locale of the vSphere Client. |
htmlSdk.app.getClientLocale();
for example: "de-DE" |
| getTheme() => PluginTheme |
Returns information about the theme the plugin should use. The returned
PluginTheme type is defined as follows:
interface PluginTheme { name: "light" | "dark" } |
htmlSdk.app.getTheme();
for example: { name: "dark" } |
| getSessionInfo(callback: (info: SessionInfo) => void) => void
(*) |
Retrieves information about the current session.
Where the info object has the following type: interface SessionInfo { sessionToken: string; } Where:
|
htmlSdk.app.getSessionInfo(
(sessionInfo) => { console.log(sessionInfo.sessionToken); }) for example: "8df9ac8f-7a3c-46d0-ab8d-89f9bf2918d9" |
| getApiEndpoints() => ApiEndpoints
(*) |
Returns an object containing the base locations of vSphere Client plugin related APIs.
The returned object is of type ApiEndpoints, where: interface ApiEndpoints { uiApiEndpoint: UiApiEndpoint; } interface UiApiEndpoint { origin: string; pathname: string; queryParams: Array<QueryParam>; fullUrl: string; } Where:
interface QueryParam { name: string; value: string; } |
htmlSdk.app.getApiEndpoints();
for example:
|
The event APIs provide means for plugins to subscribe and respond to events. These APIs can be accessed via htmlSdk.event.{API}
| API | Description | Example usage |
|---|---|---|
| onGlobalRefresh(callback: () => void) => void | Subscribes to a global refresh event and when such an event occurs, the callback function will be executed. |
function updateData() {
alert("Data is updated after a global refresh."); } htmlSdk.event.onGlobalRefresh(updateData); |
| onThemeChanged((theme: PluginTheme) => void) => void |
Subscribes for plugin theme changed events. When such an event occurs,
the callback argument function will be
invoked with a single argument - the new plugin theme in the form of
PluginTheme object.
The PluginTheme type is defined as follows: interface PluginTheme { name: "light" | "dark"; } |
function showCurrentThemeName(theme) { alert(theme.name); } htmlSdk.event.onThemeChanged(showCurrentThemeName); |