Skip to Content
Author's profile photo Vinay Godugu

How to get UserName and other login info in SAP UI5 application

Hi Everyone,

I am writing this blog to get login info into SAP UI5 application, where you can display User ID, FirstName, LastName, FullName, Email etc… in any of your SAP UI5 Apps.

Here is the sample code where you can fetch all the information into JSON format.

//Get User details once logged in

   //Get User details by using srv;

    var y = “/sap/bc/ui2/start_up”;

    var xmlHttp = null;

    xmlHttp = new XMLHttpRequest();

    xmlHttp.onreadystatechange = function() {

    if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {

       var oUserData = JSON.parse(xmlHttp.responseText);

          console.log(oUserData);

       }

    };

    xmlHttp.open( “GET”, y, false );

    xmlHttp.send(null);

or we can also use JSON Model

oModel = new sap.ui.model.json.JSONModel();

oModel.loadData(“/sap/bc/ui2/start_up”);


Note: This will only run on SAP UI5 BSP application but not on local SAP UI5 application.

Assigned Tags

      25 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo sridevi garapati
      sridevi garapati

      Hi Vinay,

      Thanks for Sharing.

      Could you please explain why it's not working in Local SAPUI5 application.

      Thanks in Advance

      Sridevi

      Author's profile photo Vinay Kumar
      Vinay Kumar
      Blog Post Author

      Hi Sridevi,

      This is not an ODATA service and it's a JS file which gives the result, so it won't work as ODATA request.

      this statement, y = "/sap/bc/ui2/start_up"; will look for this file in local system, which will not be there. So when you run this as BSP Application, that time it will look for this file in "<domain name>:<port>/sap/bc/ui2/start_up" path and it will search in gateway system.

      Hope this info helps.

      Thanks,

      Vinay

      Author's profile photo Florian Pfeffer
      Florian Pfeffer

      Hi Vinay,

      thx for sharing. What was the reason for not using the loadData method of the UI5 JSONModel (JsDoc Report - SAP UI development Toolkit for HTML5 - API Reference - sap.ui.model.json.JSONModel)? This would avoid the manual usage of the XMLHttpRequest object.

      Thx & Regards,

      Florian

      Author's profile photo Vinay Kumar
      Vinay Kumar
      Blog Post Author

      Hi Florian,

      Thank you so much 🙂 Yeah we can use JSON Model and we can avoid XMLHttpRequest object.

      Author's profile photo Suman Chakravarthy K
      Suman Chakravarthy K

      Thanks for enriching my knowledge !!

      Author's profile photo Junju Sowmya
      Junju Sowmya

      How to get Username in local ui5 applications?

      Author's profile photo Vinay Kumar
      Vinay Kumar
      Blog Post Author

      Hi Junju,

      You can try this following code by your server ip address and port (same as ODATA service)

      var oModel= new sap.ui.model.json.JSONModel();

      oModel.loadData("http://<server host>:<server port>/sap/bc/ui2/start_up");

      Author's profile photo Former Member
      Former Member

      Hi Vinay,

      How to get username in local ui5 application?

      Author's profile photo Vinay Kumar
      Vinay Kumar
      Blog Post Author

      You can try this following code by your server ip address and port (same as ODATA service)

      var oModel= new sap.ui.model.json.JSONModel();

      oModel.loadData("http://<server host>:<server port>/sap/bc/ui2/start_up");

      Author's profile photo Vinay Kumar
      Vinay Kumar
      Blog Post Author

      Hi Snigdha,

      Are you using any ODATA services in your application?

      Author's profile photo Junju Sowmya
      Junju Sowmya

      Hi Vinay,

      Thanks for the reply.

      If i give " http://<myServerName>:<port>/sap/bc/ui2/start_up ",i am getting 404-Not Found error.

      I want to get the username ,when i open the xs application from the browser.

      Your help would be highly appreciated

      Thanks,

      Sowmya

      Author's profile photo Vinay Kumar
      Vinay Kumar
      Blog Post Author

      if you don't mind what is the odata URL are u using?

      try to check in SICF transaction if /sap/bc/ui2/start_up is activated or not

      Author's profile photo Former Member
      Former Member

      Hello,
      I have the same problem. I don't have ui2 installed, we don't use ABAP. Our SAPUI5 application runs on Java AS and is authenticated via Java UME. Is there a way to get the username this way?

      Author's profile photo Sudheer Kumar Akkalapotu
      Sudheer Kumar Akkalapotu

      Hi Vinay,

      Thanks 🙂

      Author's profile photo Former Member
      Former Member

      Hi Vinay thanks for your work

      I'm trying to use this

          //Get User details by using srv;

          var y = "/sap/bc/ui2/start_up";

          var xmlHttp = null;

          xmlHttp = new XMLHttpRequest();

          xmlHttp.onreadystatechange = function() {

          if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {

             var oUserData = JSON.parse(xmlHttp.responseText);

                console.log(oUserData);

             }

          };

          xmlHttp.open( "GET", y, false );

          xmlHttp.send(null);

      the console.log show me that i get the data but then i try to set them into a JSON and then bind it to some table to display what i need.

      with this

      var oModel1 = new sap.ui.model.json.JSONModel();

                       oModel1.setData(oUserData);

                      sap.ui.getCore().setModel(aModel1,"test");

      but i get errors depending from where i put this

      either oModel1 is not defined or oUseData is not defined
      end obviously i get no data into my table

      Any ideas?

      Author's profile photo Former Member
      Former Member

      parent.sap.ushell.Container.getUser().getId()

       

      Author's profile photo Andres Bolanos
      Andres Bolanos

      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

      Author's profile photo adrian di ruggiero
      adrian di ruggiero

      Hello,

      Did you find a solution to your issue? I'm in the same situation...

      Thanks

      Adrian

      Author's profile photo Vinay Godugu
      Vinay Godugu
      Blog Post Author

      Andres Bolanos   - May I know which Fiori Cloud you using?? is it SCP Portal? because I heard Fiori cloud will be obselote in some months.

      Author's profile photo Andres Bolanos
      Andres Bolanos

      Hi Vinay Godugu I am using sapui5 on the webide, I already found a way to do it without XMLHttpRequest.

      thanks.

      Author's profile photo vijay singh
      vijay singh

      Hi Andres Bolanos 

      Could you please post your solution?

      i am also looking for a way to solve this.

       

      Thanks

      Author's profile photo RajeshReddy Bhavanam
      RajeshReddy Bhavanam

      Hi Andres Bolanos - i am also facing the same issue,can you please share the solution.

       

       

       

      Thanks,

      Rajesh

      Author's profile photo Andres Bolanos
      Andres Bolanos

      RajeshReddy Bhavanam,

      this is what worked for me

      in the neo-app.json file, add the following

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

      in the manifest.json file add the following model

      "userapi": {
      "type": "sap.ui.model.json.JSONModel",
      "settings": {},
      "uri": "/ services / userapi / currentUser",
      "preload": true
      },

      now in your controllers you can get the information as follows

      getuserid: function () {
      var modeluser = this.getOwnerComponent (). getModel ("userapi");
      return modeluser.getProperty ("/ name");
      },

      or in the xml view with the expression

      {userapi>/name}

      Greetings from Colombia

      Author's profile photo Sudhir Kancherla
      Sudhir Kancherla
      Hi
      My application is deployed to the SAP Business Technology Platform and this answer solved the problem for me.
      Thank you for sharing.
      Regards
      Sudhir
      Author's profile photo Prince Jain
      Prince Jain

      Hi,

      Thank you so much 🙂

      This solved my issue.

      Regards,

      Prince