tarzanbappa
tarzanbappa

Reputation: 4958

SAP Fiori Get logged in user details in UI5 application

I've a SAP Fiori application and I need to get the current logged in user details. I've searched web but unable to find a solution.

is there any way to get current logged in user details from launchpad.

Upvotes: 2

Views: 16760

Answers (4)

Boghyon Hoffmann
Boghyon Hoffmann

Reputation: 18044

There is a UserInfo service from the FLP shell which can be retrieved like this:

{ // In Controller
  doSomethingUserDetails: async function() {
    const userInfo = await this.getUserInfoService();
    const userId = userInfo.getId(); // And since 1.86: .getEmail(), .getFirstName(), .getLastName(), .getFullName(), ... 
    // ...
  },
  
  getUserInfoService: function() {
    return new Promise(resolve => sap.ui.require([
      "sap/ushell/library" // In the future, "sap/ushell/Container" might need to be required instead. Refer to API reference.
    ], sapUshellLib => {
      const Container = sapUshellLib.Container;
      const service = Container.getServiceAsync("UserInfo"); // .getService is deprecated!
      resolve(service);
    }));
  },
}

To align with the current best practices, avoid calling sap.ushell.Container.getService directly! Use getServiceAsync instead.

Alternatively: information about the the current user can be also retrieved via the user API service exposed by the application router from the SAP Business Technology Platform (SAP BTP).


* In case the app is deployed to the old Neo environment, see the previous edit.

Upvotes: 5

RahulDeep Attri
RahulDeep Attri

Reputation: 418

If you want to access the user detail when you are running your application in launchpad. then you can retrieve current user detail by adding following code snippet:

var userInfo = sap.ushell.Container.getService("UserInfo");
var email = userInfo.getEmail();

Then further detail about the user can be retrieved like email and fullname. See the API here.

Upvotes: 0

Boris Perlov
Boris Perlov

Reputation: 9

Try this:

new sap.ushell.services.UserInfo().getId()

Upvotes: 0

user6177399
user6177399

Reputation:

The User-ID can be retrieved from SDK. Please refer to class sap.ushell.services.UserInfo

Upvotes: 0

Related Questions