How to get logged in user (Id/user name) in UI Script?

vinuret
Kilo Contributor

Hi,

I have a requirement to run an external script (to configure appdynamics with Service portal) when the Service portal page is loaded. I have created a UI Script and added the external script content in that. I have added this script to the Portal theme. I can see that the script is running fine when the portal is loaded.

Now my query is how to get the logged in user info in the UI Script. I tried the below options.

var userName = gs.getUserName() // giving error that gs is not defined

var usreName = g_user.userName // giving error that g_user is not defined.

Can anyone please help me to sort this out? I need to get the logged in user info in my UI Script.

Thank you,

Vinu

7 REPLIES 7

Jon Barnes
Kilo Sage

do you want this to run every time somebody navigates to a different page in Service Portal or only when the first page is loaded? Because your UI script will load/run when the initial page is loaded, but will not trigger on each subsequent page as someone clicks the links because SP is a single-page application.

You can still use a UI Script, but if you want to trigger it on every page load, you may either want to put some code into a client controller to call whatever function you want to call on each page load or you can put it in your header widget, but if you do the latter, you will need to subscribe to the $locationChangeSuccess event to capture when the page changes.

Within your controllers, you can get the user details off of any controller. Make sure you pass in $scope in your controller, and you can get the user details at $scope.user.

Hi Jon,

I was trying out the basic setup and hence tried with UI Script. Is there any limitation that we cannot get the logged in user in UI Scripts? I even tried to read window.NOW.user_name property but does not seem to work in all browsers.

Thank you,

Vinu

g_user isn’t available in service portal.

Thanks Jon,

That works great! For anyone else reading this I used the below to see what the details of this object were.

console.debug("$scope.user : " + JSON.stringify($scope.user));