Skip to Content
Author's profile photo Jamie Cawley

Using the SAP HCP User API in SAP Web IDE

SAP HCP provides a service which can be used to obtain user information as well as log off the user.  The details regarding the userapi can be found at

https://help.hana.ondemand.com/help/frameset.htm?1de599bf722446849d2b2e10132df42a.html

Here’s two simple examples on how this can be acheived within SAP Web IDE.

Adding the User Details to a View

Add a route into the neo-app.json to reference the userapi


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



Create a json model to call the service


var userModel = new sap.ui.model.json.JSONModel("/services/userapi/currentUser");
sap.ui.getCore().setModel(userModel, "userapi");



Now within a view create some fields to display the data returned by the service


<Label text="User Name"></Label>
<Text xmlns="sap.m" text="{userapi>/name}"></Text>
<Label text="First Name"></Label>
<Text xmlns="sap.m" text="{userapi>/firstName}"></Text>
<Label text="Last Name"></Label>
<Text xmlns="sap.m" text="{userapi>/lastName}"></Text>
<Label text="Display Name"></Label>
<Text xmlns="sap.m" text="{userapi>/displayName}"></Text>
<Label text="Email"></Label>
<Text xmlns="sap.m" text="{userapi>/email}"></Text>



Adding a Logout Button to Your Application

In a view add a button to call a function


<Button xmlns="sap.m" text="Log Out" press="logUserOut"></Button>

Within the corresponding controller add the function to process the event.


logUserOut: function(){
    sap.m.URLHelper.redirect("logout.html", false);
}

Create a simple logout page



<!DOCTYPE HTML>
<html>
  <body class="sapUiBody" role="application">
  You have been logged out!
  </body>
</html>

Modify the neo-app.json to define the logoutPage property. Access to the logout.html does not require the user to be logged in.



"welcomeFile": "index.html",
  "logoutPage": "/logout.html",
  "routes": [
....


NOTE: When testing this it will only log you out of the application when it is deployed to HCP and in the active state.  When testing within Web IDE it will just perform the redirect without logging you out. 

Regards,

Jamie Cawley

Assigned Tags

      22 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Wolfgang Röckelein
      Wolfgang Röckelein

      Is the userapi logout still supported?

      I do not see it mentioned on https://help.hana.ondemand.com/help/frameset.htm?1de599bf722446849d2b2e10132df42a.html anymore and there is now a logout page cf https://help.hana.ondemand.com/help/frameset.htm?0c5ca8eb95214869bd1e8c5880f4b1b5.html...

      Author's profile photo Jamie Cawley
      Jamie Cawley
      Blog Post Author

      Hi Wolfgang,

      I updated the blog to reflect the change.  The userapi logout still works, but this seems to be a better approach.

      Regards,

      Jamie

      SAP - Technology RIG

      Author's profile photo Sascha Scholz
      Sascha Scholz

      Hi,

      with the old approach, the user is only logged out of the current application without ending the session on the IdP (Identity Provider, e.g. SAP ID Service). By reloading the application the application you would therefore be automatically logged in again.

      Unfortunately, the announcement in the release notes for the logout page didn't mention that the old API had been deprecated.

      Sascha

      Author's profile photo Daniel Spiess
      Daniel Spiess

      Thanks for this blog!

      However, for everybody else this might not work for right away.

      Please check this question:

      https://archive.sap.com/discussions/thread/3906841

       

      For me it also worked to get the view instead of the core:

      onInit:function(){

      var userModel = new sap.ui.model.json.JSONModel("/services/userapi/currentUser"); 

      this.getView().setModel(userModel, "userapi") ;

      }

      Author's profile photo Sharique Nasser
      Sharique Nasser

      How can we get fetch the roles assigned to this  user?

      Author's profile photo Jamie Cawley
      Jamie Cawley
      Blog Post Author

      For this api you could map the roles into user attributes as detailed at

      https://help.sap.com/viewer/65de2977205c403bbc107264b8eccf4b/Cloud/en-US/1de599bf722446849d2b2e10132df42a.html

      Regards,

      Jamie

      Author's profile photo Ishita Arora
      Ishita Arora

      I am getting unauthorized error while using this api after deploying my application to HCP. Is there any solution for this?

      Author's profile photo Jamie Cawley
      Jamie Cawley
      Blog Post Author

      Hi Ishita,

      Please create a new thread in the questions and answers section providing as much detail as possible explaining your issue.

      Regards,

      Jamie

      Author's profile photo Ishita Arora
      Ishita Arora

      Hi Jamie,

       

      https://answers.sap.com/questions/273179/unauthorized-error-in-accessing-user-api-hcp.html

      kindly refer to this question and provide a solution if possible.

      Author's profile photo Federico Bisignani
      Federico Bisignani

      This solution is not working on EXTENSION CENTER of SuccessFActors.  The API return 404

      Author's profile photo Jamie Cawley
      Jamie Cawley
      Blog Post Author

      Please create a new post in the questions and answers area and provide all of the related detail including screenshots of the configuration.

      Regards,

      Jamie

      Author's profile photo Federico Bisignani
      Federico Bisignani

      Done!

      Thanks!

      Author's profile photo Shanir Tvaija
      Shanir Tvaija

      Hi!

      I followed your instruction and "Logout" button redirects to logout.html - still no logoff is performed -    after refresh I'm still logged in. Accesing the app through URL in Fiori client.

       

      Can you help with any ideas?

      Thanks

       

      Author's profile photo Jamie Cawley
      Jamie Cawley
      Blog Post Author

      Responding to your thread in question and answers...

      Author's profile photo Chiara Ferrari
      Chiara Ferrari

      Hi Jamie!

      Thanks for this blog! I've tried your solution in my app and it's working.

      Now I need to create a widget, that displays the logged user's attributes, and add it in a cloud portal site. I’m NOT working with a trial account.

      From the web IDE I can reach the url: https://webidecp-xxxxxxxxxx.dispatcher.hana.ondemand.com/services/userapi/attributes and retrieve all the attributes

      From the portal I get an error 404 not found: https://flpnwc-xxxxxxxxxx.dispatcher.hana.ondemand.com/services/userapi/attributes

      I guess I need to change some configuration in my neo-app.json or manifest.json, do you have any suggestion on how to do that?

      Thanks in advance,

      Chiara

      Author's profile photo Jamie Cawley
      Jamie Cawley
      Blog Post Author

      This is expected.  When an app is registered to FLP, the url to a service is in a different format.  It should looks something like

      https://flpnwc-xxxxx.ondemand.com/<the registered app’s namespace>/services/userapi/attributes

      You shouldn't have to change anything to make this work, but maybe you have some problem somewhere.

      Regards,

      Jamie

      Author's profile photo Edoardo Bonizzoni
      Edoardo Bonizzoni

      Hello,

      I can't find documentation on how to create the destination endpoint "userapi" in SCP cockpit.

      Could you point me in the right direction?

       

      I'm using the Identity Authentication service as Identity Authentication Tenant in Trust Manager.

       

      Regards,

      Edoardo

       

      Author's profile photo Jamie Cawley
      Jamie Cawley
      Blog Post Author

      Hi Edoardo,

      You do not need to create a destination, it's a service that is available directly.

      Regards,

      Jamie

      Author's profile photo Akshay Vinod Punnath
      Akshay Vinod Punnath

      Hello Jamie,

      I have similar issue as Edoardo.

      Have configured SSO for my application host in CF landscape and people are able to login.

      What method can I use to identify who has logged in? userapi doesnt work in CF.

      Regards,

      Akshay

       

      Author's profile photo Jamie Cawley
      Jamie Cawley
      Blog Post Author

      Hi Akshay,

      You can find related answers to your issue in the questions and answers area for example

      https://answers.sap.com/questions/746055/how-to-configure-userapi-service-in-cloudfoundry.html

      Regards,

      Jamie

      Author's profile photo Ivan Jassir
      Ivan Jassir

      Hi! great post!.

      it works well on web apps but when I create a Hybrid application it doesnt work, do you know how to solve this issue ?

      Thanks in advaced!.

      Best Regards,

      Ivan

      Author's profile photo Jamie Cawley
      Jamie Cawley
      Blog Post Author

      HI Ivan,

      I would suggest opening a message in the questions and answers area with the full details of your scenario.

      Regards,

      Jamie