2

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.

Boghyon Hoffmann
  • 17,103
  • 12
  • 72
  • 170
tarzanbappa
  • 4,930
  • 22
  • 75
  • 117

4 Answers4

5

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

{ // In Controller
  doSomethingUserDetails: async function() {
    const oUserInfo = await this.getUserInfoService();
    const sUserId = oUserInfo.getId(); // And in SAPUI5 1.86, those became public: .getEmail(), .getFirstName(), .getLastName(), .getFullName(), ... 
    // ...
  },
  
  getUserInfoService: function() {
    return new Promise(resolve => sap.ui.require([
      "sap/ushell/library"
    ], oSapUshellLib => {
      const oContainer = oSapUshellLib.Container;
      const pService = oContainer.getServiceAsync("UserInfo"); // .getService is deprecated!
      resolve(pService);
    }));
  },
}

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

  • getService is deprecated. Use getServiceAsync instead.
  • Require the library instead of referencing the Container via global namespace (sap.ushell.*) as shown above.

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.

Boghyon Hoffmann
  • 17,103
  • 12
  • 72
  • 170
0

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

0

Try this:

new sap.ushell.services.UserInfo().getId()
Adrita Sharma
  • 21,581
  • 10
  • 69
  • 79
  • 1
    API reference explicitly warns NOT to call the constructor directly: "[new ...] must be called by the Unified Shell's container only, others MUST call `sap.ushell.Container.getServiceAsync("UserInfo")`." Something like this: https://stackoverflow.com/a/63524439/5846045 – Boghyon Hoffmann Aug 21 '20 at 14:11
0

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.

RahulDeep Attri
  • 398
  • 3
  • 13