cancel
Showing results for 
Search instead for 
Did you mean: 

Best way to design the return of an Employee Entity for current logged in User?

MattHarding
Active Contributor
0 Kudos

Hi All,

I want to return an employee entity for the currently logged in User for a UI5 app. Now since my employee entity has a username filter, I could call service/employees(filter=Username eq 'myuname') but I'm not sure how easy it is for a UI5 app to generically work out the user id since authentication could be done in numerous ways in the browser.

Hence I want to provide the ui5 development a service to return an employee entity for the current logged in user.

Question: How should I do this?

Option a: Create a function import called GetCurrentLoggedInEmployee that returns an Employee entity.


This is very obvious and probably what I'm thinking is the right way.


Option b: Call a generic Gateway service that exists that I don't know about that returns the username???


Option c: ??? Pretty sure I can't navigate from an Employees entity so struggling to think of other nice options.


Please note: I haven't looked at how Fiori apps do this which could hold the key.


Thanks for any advice you can give,

Matt

Accepted Solutions (1)

Accepted Solutions (1)

ChandraMahajan
Active Contributor
0 Kudos

Hi Matt,

Option A looks perfect for me but same can be done by implementing get_entity for employee entity. now you can make userID as key and while querying, you can pass it as

EmployeeCollection(UserID=' ') and then just ignore the key passed as you will be referring userid as

sy-uname. This way you can make it generic.

2nd option, there is SICF service /sap/bc/ui2/start_up which returns all the required logged in user details. you can query with /sap/bc/ui2/start_up in GW Client transaction to verify the details. but as this is not OData service but SICF service returning data in the fromat of JSON, you may want to execute ajax call to get the details.

Regards,

Chandra

MattHarding
Active Contributor
0 Kudos

Thanks Chandra. Using the SICF service fits best with my thinking though the blank userID is a nice idea, though not intuitively obvious which I try to avoid those scenarios.

Cheers,
Matt

kammaje_cis
Active Contributor
0 Kudos

Creating a new entity to return just the sy-uname's information would be very simple. If you do not want to pass empty username, you have two options. Return the information in get_entityset. Other option is to use a function import, which will return a single record.

I checked in the FIori and it uses the '/sap/bc/ui2/start_up' service.

ChandraMahajan
Active Contributor
0 Kudos

Hi Matt,

And also if you just want to display logged in user details without Ajax call to start_up service then you can use below coding in UI5 application.

basically it will call startup SICF service only.


<!DOCTYPE HTML>

<html>

  <head>

  <meta http-equiv="X-UA-Compatible" content="IE=edge">

  <script src="/sap/public/bc/ui2/services/sap/ui2/srvc/error.js"></script>

  <script src="/sap/public/bc/ui2/services/sap/ui2/srvc/utils.js"></script>

  <script src="/sap/public/bc/ui2/shell-api/sap/ui2/shell/startup.js"></script>

      <script src="https://sapui5.netweaver.ondemand.com/resources/sap-ui-core.js"

  id="sap-ui-bootstrap"

  data-sap-ui-libs="sap.ui.commons"

  data-sap-ui-theme="sap_goldreflection">

  </script>

  <!-- add sap.ui.table,sap.ui.ux3 and/or other libraries to 'data-sap-ui-libs' if required -->

  <script>

  var oUser = sap.ui2.shell.getUser();

  oUser.load({}, function(){

   var fullName = oUser.getFullName();

   var userID = oUser.getId();

   //alert(fullName);

   //alert(userID);

  //create the ApplicationHeader control

  var oAppHeader = new sap.ui.commons.ApplicationHeader("appHeader");

  //configure the branding area

  oAppHeader.setLogoSrc("http://www.sap.com/global/images/SAPLogo.gif");

  oAppHeader.setLogoText("User Details");

  //configure the welcome area

  oAppHeader.setDisplayWelcome(true);

  oAppHeader.setUserName(fullName);

  //configure the log off area

  oAppHeader.setDisplayLogoff(true);

  oAppHeader.placeAt("content");

  },

  function(){

   alert ('Error');

  });

  </script>

  </head>

  <body class="sapUiBody" role="application">

  <div id="content"></div>

  </body>

</html>

and as Krishna rightly mentioned these user services are being used in Fiori.

Regards,

Chandra

MattHarding
Active Contributor
0 Kudos

Thanks Krishna and Chandra. I think between using the /sap/bc/ui2/start_up service or the sap.ui2.shell.getUser we have the right answer, and I can just return the Employee using a filter of username since that makes the model intuitive.

Thanks again,

Matt

Answers (0)