give me sapui5 code to navigate to home page of launchpad
- User
give me sapui5 code to navigate to home page of launchpad
- Assistant
To navigate to the home page of the SAP Fiori Launchpad from a SAPUI5 application, you can use the services provided by the Launchpad. Below are two common methods to achieve this:
- Using the CrossApplicationNavigation Service
The
CrossApplicationNavigationservice allows you to perform cross-application navigation within the Fiori Launchpad.// Get a reference to the CrossApplicationNavigation service var oCrossAppNavigator = sap.ushell.Container.getService("CrossApplicationNavigation"); // Check if the service is available if (oCrossAppNavigator) { // Navigate to the Launchpad home page oCrossAppNavigator.toExternal({ target: { shellHash: "#" } }); }Explanation:
sap.ushell.Container.getService("CrossApplicationNavigation")retrieves the cross-application navigation service.- The
toExternalmethod is used to navigate to a specific hash within the Launchpad. - Setting
shellHashto"#"redirects the user to the Launchpad home page.
- Using the ShellUIService
The
ShellUIServiceprovides high-level navigation functions, including a method to navigate to the home page.// Asynchronously get the ShellUIService sap.ushell.Container.getServiceAsync("ShellUIService").then(function (oShellUIService) { // Navigate to the Launchpad home page oShellUIService.navigateToShellHome(); });Explanation:
getServiceAsync("ShellUIService")retrieves theShellUIServiceasynchronously.- Once the service is available,
navigateToShellHome()is called to navigate back to the home page.
Additional Notes:
- Ensure that your SAPUI5 application is running within the Fiori Launchpad. The
sap.ushellnamespace is only available when the application is launched from the Launchpad. - If you are testing the application outside the Launchpad (e.g., running it standalone), the
sap.ushell.Containerwill not be available, and you'll need to handle such cases to prevent errors.
Example of Handling Non-Launchpad Environments:
if (sap.ushell && sap.ushell.Container) { // Proceed with navigation } else { // Fallback or error handling for standalone mode }Usage in an SAPUI5 Controller:
Here's how you might incorporate the navigation code within a controller's method:
sap.ui.define([ "sap/ui/core/mvc/Controller" ], function (Controller) { "use strict"; return Controller.extend("my.namespace.controller.MyController", { onNavigateHome: function () { if (sap.ushell && sap.ushell.Container) { sap.ushell.Container.getServiceAsync("ShellUIService").then(function (oShellUIService) { oShellUIService.navigateToShellHome(); }); } else { sap.m.MessageToast.show("Navigation service is not available."); } } }); });In this example:
onNavigateHomeis a method that can be bound to a button press or any other event.- The code checks if the
sap.ushell.Containeris available before attempting to navigate.
References:
By using one of these methods, you can programmatically navigate users back to the Fiori Launchpad home page from your SAPUI5 application.