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.
Hi Vinay,
Thanks for Sharing.
Could you please explain why it's not working in Local SAPUI5 application.
Thanks in Advance
Sridevi
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 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
Hi Florian,
Thank you so much 🙂 Yeah we can use JSON Model and we can avoid XMLHttpRequest object.
Thanks for enriching my knowledge !!
How to get Username in local ui5 applications?
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");
Hi Vinay,
How to get username in local ui5 application?
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 Snigdha,
Are you using any ODATA services in your application?
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
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
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?
Hi Vinay,
Thanks 🙂
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?
parent.sap.ushell.Container.getUser().getId()
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
Hello,
Did you find a solution to your issue? I'm in the same situation...
Thanks
Adrian
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.
Hi Vinay Godugu I am using sapui5 on the webide, I already found a way to do it without XMLHttpRequest.
thanks.
Hi Andres BolanosÂ
Could you please post your solution?
i am also looking for a way to solve this.
Thanks
Hi Andres Bolanos - i am also facing the same issue,can you please share the solution.
Thanks,
Rajesh
RajeshReddy Bhavanam,
in the neo-app.json file, add the following
in the manifest.json file add the following model
now in your controllers you can get the information as follows
or in the xml view with the expression
Greetings from Colombia
Hi,
Thank you so much
This solved my issue.
Regards,
Prince
Hi Andres Bolanos and Sudhir Kancherla
In SAP Business application studio, we don't have neo-app.json, can you suggest what code i should write in xs-app.json to get the user please.
Thank you so much for detailed solution Andres BolanosÂ
Solution worked for my BTP Fiori app hosted in NEO