Newer Version Available
Lightning Console JavaScript API Syntax
To use the Lightning Console JavaScript API, include lightning:workspaceAPI, lightning:utilityBarAPI, or both in your Lightning component.
The lightning:workspaceAPI component gives you access to the Workspace API, while the lightning:utilityBarAPI component gives you access to the Utility Bar API. Give each component an aura:id so that you can reference it from the component’s controller.
The follow example shows a simple Lightning component that uses both libraries:
1<aura:component implements="flexipage:availableForAllPageTypes" access="global">
2 <lightning:workspaceAPI aura:id="workspace" />
3 <lightning:utilityBarAPI aura:id="utilitybar" />
4 <lightning:button label="Open Utility" onclick="{! c.openUtilityBar }"/>
5 <lightning:button label="Open Tab" onclick="{! c.openTab }" />
6</aura:component>This component implements flexipage:availableForAllPageTypes so that it can be accessed in the Lightning App Builder.
This is the component’s JavaScript controller:
1({
2 openUtilityBar : function(component, event, helper) {
3 var utilityAPI = component.find("utilitybar");
4 utilityAPI.openUtility();
5 }
6
7 openTab : function(component, event, helper) {
8 var workspaceAPI = component.find("workspace");
9 workspaceAPI.openTab({
10 url: ‘#/sObject/001R0000003HgssIAC/view’,
11 focus: true
12 }).then(function(response) {
13 workspaceAPI.getTabInfo({
14 tabId: response
15 }).then(function(tabInfo) {
16 console.log(“The recordId for this tab is: “ + tabInfo.recordId);
17 });
18 })
19 .catch(function(error) {
20 console.log(error);
21 });
22 }
23})The controller has two functions, each of which uses an API method. To use a method in a controller, use component.find with the aura:id you gave to the lightning:workspaceAPI or lightning:utilityBarAPI.
Methods in the Workspace API and the Utility Bar API take a JSON object as an argument. The values included in the object depend on the method. openTab, for example, takes an object that includes the url and focus (whether the new tab has focus). Check the reference section of this guide before using a method so that you know which arguments to pass to it.