Skip to Content
Technical Articles
Author's profile photo Rajat Goyal

Getting User Information in SAPUI5 application on WEB IDE

One of the common issues I see developers struggling with is to get the logged in user details in SAPUI5 application.

Suppose there is a requirement under which you want show logged in user’s details in your SAPUI5 application like username or user ID or user email etc. Then I have seen many developers not finding any working solution on client side (FE) for the problem and doing and a backend call to fetch the user details.

SAP HANA Cloud Platform provides a service called User API which can provide the user information on SAP WEB IDE without doing an explicit backend OData call. For more information about the service, you can refer SAP HANA Cloud Platform documentation.

This API provide the logged in user details at the application level from SAP HANA Cloud Platform. By using this API you can see following details:-

  • name (User ID)
  • Display Name (Full name with User ID)
  • Email
  • First name
  • Last Name

 

Now let us see the step by step guide to include and use this user API in your Application.

 

  • First add the below given code snippet in your application’s neo-app.json

    neo-app.jsonThis will add a route for SAP HANA Cloud Platform user API in the application.
    The text code is also included to make it easier to use…

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

     

  • Create a JSON model to consume the service.In this example I am creating a global model so that the user information is available throughout the application.model.js
    Above example will create a global model over the User API service to consume the current logged in user details.

    createUserModel: function(){
    			var oModel = new JSONModel("/services/userapi/currentUser");
    			return oModel;
    		}
    

     

  • Now we will set this model in Component.jsComponent.jsNow the model is set, and we can use it in our views and controllers as per the requirement.
    this.setModel(models.createUserModel(), "userModel");

     

    NOTE: – You can also create the model directly in Manifest.json file or if only needed in single view, then you can also create a local model. It completely depends on your requirement

    The model data will contain following information as shown in image below: –Final%20Output
    As you can see all the basic user information is available to use as per requirement.

 

 

 

This way you can easily fetch the user details in your application without any extra backend call. It is way faster and and  more efficient way of fetching the user details of whoever is currently using the application. The data will only contain a single object with 5 key-value pairs as shown above in the last step or mentioned at the beginning  of the post.

 

 

NOTE:– Only the Current User details can be fetched using this API. It will not provide details about all user who has accessed the application.

 

Regards,
Rajat Goyal

 

Assigned tags

      16 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Urvashi Tibrewal
      Urvashi Tibrewal

      Great Blog .. You have explained the concept in a simple and elegant manner.

      Author's profile photo Rajat Goyal
      Rajat Goyal
      Blog Post Author

      Thank you Urvashi

       

      Author's profile photo SAHITYA RATAN
      SAHITYA RATAN

      Thanks..the API was great help

      Looking forward to more such blogs

      Author's profile photo Rajat Goyal
      Rajat Goyal
      Blog Post Author

      Thank you Sahitya

      Author's profile photo Oshin Kandpal
      Oshin Kandpal

      Nice blog... Explained it so well

      Author's profile photo Rajat Goyal
      Rajat Goyal
      Blog Post Author

      Thank you Oshin

      Author's profile photo Shubham Kandpal
      Shubham Kandpal

      Great blog.. Thanks for sharing this useful API..

      Author's profile photo Rajat Goyal
      Rajat Goyal
      Blog Post Author

      Thank you Shubham

      Author's profile photo Cristiano Marques
      Cristiano Marques

      Great blog Rajat Goyal .

       

      But how  can I get user info in opremisse s4hana?

      Author's profile photo Rajat Goyal
      Rajat Goyal
      Blog Post Author

      Hi Cristiano Marques ,

      This API is available on SAP HANA Cloud Platform.

      Can you tell which type of application you are developing and where exactly it is deployed?

      Author's profile photo Cristiano Marques
      Cristiano Marques

      I am developing transactional applications using sap web ide and visual studio code. It is deployed in s4hana onpremise using cloud connector with scp neo environment

      (I don´t get yet how to connect SCP cloud foundry to cloud connector)

      Author's profile photo anitra carol
      anitra carol

      Useful Information, your blog is sharing unique information.
      Thanks for sharing!!!

      Author's profile photo Rajat Goyal
      Rajat Goyal
      Blog Post Author

      Thank you for the complement.

      Author's profile photo Ajinkya Jadhav
      Ajinkya Jadhav

      Hi Rajat Goyal ,

      Great Blog! Thank You.

      Do we have anything in SAP HANA XSA on premise version to get the user information?

      Currently we are doing the backend call for this.

      Do we have similar kind of approach for MTA Applications as well?

      Thank You.

      Best Regards,

      Ajinkya Jadhav

      Author's profile photo Minjie Lao
      Minjie Lao

      Hi,

       

      I am developing the SAP UI5 webapp project in BAS, instead WEB IDE, my project is using manifest.json as the configuration file.

       

      Is there a way to configure the user api?

       

      Author's profile photo Rajat Goyal
      Rajat Goyal
      Blog Post Author

      Hi,

       

      You can configure the resource in xs-app.json and then create a model in manifest.json using the dataSource of the resource.

      Although i have not not tried it with BAS yet, so i am not 100% sure whether it will work. But ideally it should.

       

      Thanks & Regards,

      Rajat Goyal