Lightning Web Components (LWC) allows developers to interact with Salesforce console applications using the WorkSpace API. This API helps in managing tabs and subtabs dynamically, imporving user experience and productivity.
In this article, we’ll explore the Platform WorkSpace API in LWC and how it helps control workspace tabs and subtabs within a Lightning console app.
Importing the WorkSpace API
To access various API methods, you need to import the lightning/platformWorkSpaceAPI
module. These methods allow you to open, close, refresh, and manage tabs, improving workflow efficiency.
Here is an example of one of the wire adapter isConsoleNavigation which is used to determine if component is in Lightning Console App or not. Below is the example code.
But before that remember all the methods of Platform WorkSpace API returns a promise.
import { LightningElement,wire} from 'lwc'; import {IsConsoleNavigation} from 'lightning/platformWorkspaceApi' export default class WorkspaceAPIDemo extends LightningElement { @wire(IsConsoleNavigation) isConsoleNavigation }
<template> <lightning-card> <p>{isConsoleNavigation}</p> </lightning-card> </template>
If you drop the above component in Lightning console app then isConsoleNavigation will have true value else it will have false.
Available Methods in Platform WorkSpace API
Here’s a list of the available methods in the platformWorkSpaceAPI
module:
- closeTab() – Closes a specific tab.
- disableTabClose() – Disables the close option for a tab.
- focusTab() – Brings a specific tab into focus.
- getAllTabInfo() – Retrieves information about all open tabs.
- getFocusedTabInfo() – Gets details of the currently focused tab.
- getTabInfo() – Fetches information about a particular tab.
- openSubtab() – Opens a subtab under a parent tab.
- openTab() – Opens a new primary tab.
- refreshTab() – Refreshes a specific tab.
- setTabHighlighted() – Highlights a tab for emphasis.
- setTabIcon() – Sets an icon for a tab.
- setTabLabel() – Updates the label of a tab.
Wire Adapters in WorkSpace API
In addition to methods, the WorkSpace API provides wire adapters for real-time interaction:
- EnclosingTabId – Retrieves the ID of the parent tab.
- IsConsoleNavigation – Checks if console navigation is enabled.
We have already learned about isConsoleNavigation. This article only serves as an introduction to Platform WorkSpace API in LWC we will look at all the methods in the future article.