cancel
Showing results for 
Search instead for 
Did you mean: 

SAPUI5 - how to get loged User ID?

Former Member

Hi experts,

I have problem, when I try to get the ID from the loged user in SAPUI5.

I have tried:

1.

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

     var userId = oUser.getId();

Here I have error: Uncaught TypeError: Cannot read property 'shell' of undefined

2.

     var oUser = new sap.ushell.services.UserInfo();
     var userId = oUser.getId();

Here I have error: Uncaught TypeError: Cannot read property 'services' of undefined

I will appreciate if you can show me an example or to guide me, beacause I don't know what I'm doing wrong.

BR,

Simona

boghyon
Product and Topic Expert
Product and Topic Expert
0 Kudos

For future readers: please use `getServiceAsync` instead. The current accepted answer is outdated as the `getService` API is deprecated.

Accepted Solutions (1)

Accepted Solutions (1)

former_member182329
Participant

Hi Simona,

                You can retrieve the logged in Username by calling the shell service " sap.ushell.Container.getService("UserInfo").getId() ". Its mentioned the UI5 Api  Refrence page (SAPUI5 SDK - Demo Kit).

Regards,

Bince

I am getting sap.ushell.Container.getService("UserInfo").getUser() as undefined

Answers (12)

Answers (12)

Khushdeep
Explorer

HI,

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:

sap.ushell.Container.getUser()

I hope this is enough to get a user related object. then further detail about the user can be retrieved.

Regards,

Khushdeep

mahesh_z
Participant

Hi,

Just like what you have tried , you can use :

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

oUser.load({}, function() {

var userID = oUser.getId();

}

and you need to do this inside your index.html:

<script src="/sap/public/bc/ui2/services/sap/ui2/srvc/error.js"></script>

<script src="/sap/public/bc/ui2/services/sap/ui2/srvc/utils.js"></script>

<script src="/sap/public/bc/ui2/shell-api/sap/ui2/shell/startup.js"></script>

Thanks,

Mahesh Z.

Hi Simona,


Solution 2: create one oData in SEGW to fetch currrent user then you can read the odata in your SAPUI5 application in any event like oninit () or any other function.

               use the code write : sy-UNAME. in class or RFC


Regards

Sathish Mani


0 Kudos

None of the above methods worked for me. I am getting

Uncaught (in promise) TypeError: Cannot read property 'Container' of undefined

Any idea?

former_member675984
Participant
0 Kudos

Hi,

I could not get the user id, I have tried in the following ways:

1. sap.ui2.shell.getUser ();

2. sap.ushell.Container.getService (" UserInfo ");

3. adding the scripts to the index file

4. with xmlHttp

5. sap.ui.require ([ 'sap / ushell_abap / bootstrap / abap' ], ...

but nothing works for me, some give me the error "of undefined", others print "default_user". but not the user id, it's something like "S0021 ..."

I am working with SAP Fiori Cloud

I appreciate your collaboration

0 Kudos

Hi,you also can get all the system information (user Id, fullname, system, ...) from the config.

sap.ui.require([ 
	'sap/ushell_abap/bootstrap/abap'
], function () {
	var sysinfo = window["sap-ushell-config"].services.Container.adapter.config;
	console.log(sysinfo.id);
});

Regards,
Mario

Former Member
0 Kudos

Hi Simona,

You can follow below URL, hope it helps

Thanks,

Vin

karthikarjun
Active Contributor
0 Kudos

I hope this will help you.

Regards,

Karthik A

SergioG_TX
Active Contributor
0 Kudos

Simona,

the easiest way would be to do an ajax request to an xsjs service and ask the Session API for the logged in user or if you have SAML enabled you can also verify that from the XSJS side.

check out the documentation here.

http://help.sap.com/hana/SAP_HANA_XS_JavaScript_Reference_en/$.Session.html

hope this helps

0 Kudos

Hi Simona,

Just in case that you are still looking for an answer - if you need just the username, then create a XS service (.xsjs) :

$.response.contentType = "application/json";

var output = "";

var userName = $.session.getUsername();

output = '{"username":"'+userName+'"}';

$.response.setBody(output);

and call it in your view via an $.ajax call.

Greetings from Sofia,

Martin

0 Kudos

Hi Simona,


Solution 1: you can read the oModel or JsonModel and in that there is a property is called sUser . so You can read the value of sUser then you will get the logged user name. see the bellow image

Regards

Sathish Mani


Former Member
0 Kudos

Hi Sathish,

I tried the solution 1 but its not working I'm not getting the sUser, it's showing as undefined

any suggestions are appreciated...

Regards,

SaNthoSh

0 Kudos

This code just gets the DB userId not the portal userId, right?

SandipAgarwalla
Active Contributor
0 Kudos

Where r u hosting your Ui5 app?  ABAP or any other java server?

The Ui2 scripts will work only in case the app is hosted on ABAP with the Ui5 Add-On)

former_member217383
Discoverer
0 Kudos

Hi Sandip,

What are the .js files need to added to local ui5 application to get the username?

My app is not hosted on any ABAP .

your help would br highly appreciated..

Thanks

ChandrashekharMahajan
Active Contributor
0 Kudos

Hi Simona,

Refer this thread discussion

I guess you need to add below js files,

<script src="/sap/public/bc/ui2/services/sap/ui2/srvc/error.js"></script>

<script src="/sap/public/bc/ui2/services/sap/ui2/srvc/utils.js"></script>

<script src="/sap/public/bc/ui2/shell-api/sap/ui2/shell/startup.js"></script>

<script src="/sap/public/bc/ui2/shell-api/sap/ui2/shell/shell.js"></script>

Regards,

Chandra