
The application router exposes a user API that returns the details of the user who is logged in. This API supports two endpoints:/currentUser
and/attributes
. The /currentUser endpoint returns all details of logged in users, while the /attributes endpoint returns the main user properties.
yo easy-ui5 project
{
"source": "^/user-api/currentUser$",
"target": "/currentUser",
"service": "sap-approuter-userapi"
},
/user-api/currentUsers
returns user info in the following structure.{
"firstname": "John",
"lastname": "Doe",
"email": "john.doe@sap.com",
"name": "john.doe@sap.com",
"displayName": "John Doe (john.doe@sap.com)"
}
sap.ui.define([
"demo/userapi/controller/BaseController",
"sap/ui/model/json/JSONModel"
], function (Controller, JSONModel) {
"use strict";
return Controller.extend("demo.userapi.controller.MainView", {
onInit: function () {
this.getUserInfo();
},
getUserInfo: function () {
const url = this.getBaseURL() + "/user-api/currentUser";
var oModel = new JSONModel();
var mock = {
firstname: "Dummy",
lastname: "User",
email: "dummy.user@com",
name: "dummy.user@com",
displayName: "Dummy User (dummy.user@com)"
};
oModel.loadData(url);
oModel.dataLoaded()
.then(()=>{
//check if data has been loaded
//for local testing, set mock data
if (!oModel.getData().email) {
oModel.setData(mock);
}
this.getView().setModel(oModel, "userInfo");
})
.catch(()=>{
oModel.setData(mock);
this.getView().setModel(oModel, "userInfo");
});
},
getBaseURL: function () {
var appId = this.getOwnerComponent().getManifestEntry("/sap.app/id");
var appPath = appId.replaceAll(".", "/");
var appModulePath = jQuery.sap.getModulePath(appPath);
return appModulePath;
},
});
});
/6933944e-429d-4c98-8fb1-24d04de767f7.userapiservice.demouserapi/~010821071004+0000~
') before the URL you're actually calling. Without the base URL, the call will fail with Not Found (404) error. This is not specific to User API, but applies to any other URLs. <mvc:View controllerName="demo.userapi.controller.MainView"
displayBlock="true"
xmlns="sap.m"
xmlns:l="sap.ui.layout"
xmlns:f="sap.ui.layout.form"
xmlns:mvc="sap.ui.core.mvc">
<App id="idAppControl" >
<pages>
<Page title="{i18n>title}">
<content>
<VBox class="sapUiSmallMargin">
<f:SimpleForm id="SimpleFormDisplay354"
editable="false"
layout="ResponsiveGridLayout"
title="User Info"
labelSpanXL="3"
labelSpanL="3"
labelSpanM="3"
labelSpanS="12"
adjustLabelSpan="false"
emptySpanXL="4"
emptySpanL="4"
emptySpanM="4"
emptySpanS="0"
columnsXL="1"
columnsL="1"
columnsM="1"
singleContainerFullSize="false" >
<f:content>
<Label text="First Name" />
<Text text="{userInfo>/firstname}" />
<Label text="Last Name" />
<Text text="{userInfo>/lastname}" />
<Label text="Email" />
<Text text="{userInfo>/email}" />
<Label text="Display Name" />
<Text text="{userInfo>/displayName}" />
</f:content>
</f:SimpleForm>
</VBox>
</content>
</Page>
</pages>
</App>
</mvc:View>
npm run deploy
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
7 | |
7 | |
7 | |
6 | |
5 | |
5 | |
5 | |
5 | |
5 | |
4 |