cancel
Showing results for 
Search instead for 
Did you mean: 

How to get the user principle of logged user in UI5?

0 Kudos

Hi experts,

I have developed a custom standalone UI5 application which will run on HCP (CP/CF).

My Requirement is to get current logged user principle without any java code.

is there any possibility to get user principle in UI5 application.

If yes pls guide me on this issue.

Thanx and Regards

Rishabh

Accepted Solutions (1)

Accepted Solutions (1)

zeba1
Explorer

Hi Rishabh,

Please use Third party API to get the principal users .

OR

SAP HCP provides a service which can be used to obtain user information as well as log off the user. The details regarding the userapi can be found at

https://help.hana.ondemand.com/help/frameset.htm?1de599bf722446849d2b2e10132df42a.html

Here’s is the example on how this can be acheived within SAP Web IDE.

Adding the User Details to a View

Add a route into the neo-app.json to reference the userapi


{
        "path": "/services/userapi",
        "target": {
            "type": "service",
            "name": "userapi"
        }
    }

Create a json model to call the service


var userModel = new sap.ui.model.json.JSONModel("/services/userapi/currentUser");
sap.ui.getCore().setModel(userModel, "userapi");

Now within a view create some fields to display the data returned by the service


<Label text="User Name"></Label>
<Text xmlns="sap.m" text="{userapi>/name}"></Text>
<Label text="First Name"></Label>
<Text xmlns="sap.m" text="{userapi>/firstName}"></Text>
<Label text="Last Name"></Label>
<Text xmlns="sap.m" text="{userapi>/lastName}"></Text>
<Label text="Display Name"></Label>
<Text xmlns="sap.m" text="{userapi>/displayName}"></Text>
<Label text="Email"></Label>
<Text xmlns="sap.m" text="{userapi>/email}"></Text>

Answers (5)

Answers (5)

WouterLemaire
Active Contributor
0 Kudos

This blog helped for me https://blogs.sap.com/2019/05/23/how-to-get-the-email-of-the-logged-in-user-in-cloud-foundry/

The blog uses the mail address as example but you can get more info about the user if needed.


kr, Wouter

0 Kudos

How to get logged in user info in SAP UI5 deployed in CF.

AshmitaSinha
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hello Ajay,

I haven't tried the below snippet,

var oUser = sap.ui2.shell.getUser();

oUser.load({}, function() {

var userID = oUser.getId();

}

Try if it works.

Regards,

Ashmita Sinha

AshmitaSinha
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hello Rishabh,

Are you able to use the same "userapi" service for the projects which will run on CP and CF?

I have used this service in a project and deployed the same in CF, but it fails to get data.

0 Kudos

Hi Ashmita,

Did you able to get username ?

Please help

Regards

Ajay

0 Kudos

Hi Rishabh,

try this.

var oUser = sap.ui2.shell.getUser();

oUser.load({}, function() {

var userID = oUser.getId();

}