Skip to Content

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.

23 Comments
You must be Logged on to comment or reply to a post.
    • 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

    • 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“);

    • 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“);

  • Hi Vinay,

    Thanks for the reply.

    If i give ” http://<myServerName&gt;:<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

  • 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?

  • 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

  • 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