Technical Articles
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)
- 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
This 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.
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.js
Now 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: –
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
Great Blog .. You have explained the concept in a simple and elegant manner.
Thank you Urvashi
Thanks..the API was great help
Looking forward to more such blogs
Thank you Sahitya
Nice blog... Explained it so well
Thank you Oshin
Great blog.. Thanks for sharing this useful API..
Thank you Shubham
Great blog Rajat Goyal .
But how can I get user info in opremisse s4hana?
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?
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)
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
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?
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
Hello thanks for the blog
We are getting started in the world of BTP. A small SAP UI5 application was developed that queries Success Factor Employee Central, the application requests a badge and retrieves data. And in development environment, everything is fine when you publish it. The next step is to integrate it into SF EC. Can you give me an idea how to perform the tasks:
1. When from SF EC, invoke the SAP UI5 application, do not ask for the user password
Hello Rajat,
Nice blog with useful information, explained in a simple manner.
Looking forward for a lot like this.
Thanks and Regards,
Veera Sudheer