work item customization: getting current user as logged in user
Anna Phan (11●2)
| asked Jun 07 '16, 3:35 a.m.
converted to question Jun 07 '16, 3:44 a.m. by Donald Nong (14.5k●6●14)
Hello,
Such a long time but my problem is still the same.
I am trying this code below to catch the current user id. This script only works when i change the dependent attributes. But when a work item is saved, the script is recalculated and it doesn't work, the return is null.
What could be a problem?
dojo.provide("testImport.setCurrentUser"); dojo.require("com.ibm.team.repository.web.client.session.Session"); |
2 answers
Note that the script only works in a browser. If you are using a calculated value provider, it may be executed on the server side, which will not work.
Comments Thank Donald.
I tested this script in a browser. This script only works when the dependent attributes changed. The console.log() function shows that the script is not called when a new a work item is created or when a work item is saved
Ralph Schoon
commented Jun 07 '16, 6:32 a.m.
| edited Jun 07 '16, 6:34 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER
Yes, a calculated value provider only works under certain conditions. The most important is that it must detect a change. It is calculated from some attribute change.
Hmm, from https://jazz.net/wiki/bin/view/Main/AttributeCustomization#Calculated_values
Depending on the presentations of the two attributes this may not work in all cases.
Anna Phan
commented Jun 07 '16, 9:17 p.m.
Hi Ralph,
Information from your link https://jazz.net/wiki/bin/view/Main/AttributeCustomization#Calculated_values, I understand that the script must be executed in all three cases, not one of them.
Ralph Schoon
commented Jun 08 '16, 2:11 a.m.
| edited Jun 08 '16, 2:12 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER
And the scripts are executed in all these cases, except if you use the unsupported API you are using. So it is the usage of the unsupported API that breaks the scripts. Maybe surrounding the unsupported call in a try/catch block could help. I don't have the time to debug that. I found to my satisfaction that a correct script without using unsupported API gets called in the above scenarios. For example this script does:
|
Ralph Schoon (63.6k●3●36●46)
| answered Jun 07 '16, 9:21 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER
It is the (unsuported) session related code below that prevents the script to run on work item creation:
var Session = com.ibm.team.repository.web.client.session.Session; var getAuthenticatedContributor = com.ibm.team.repository.web.client.session.getAuthenticatedContributor; var userId = getAuthenticatedContributor().userId; This code works: dojo.provide("com.example.ValueProvider"); dojo.require("com.ibm.team.repository.web.client.session.Session"); (function() { dojo.declare("com.example.ValueProvider", null, { getValue: function(attribute, workItem, configuration) { var userId="Test"; var out = "TestScriptRAS VP: " +(new Date()).getTime() + " " + userId; console.log( out ); return out ; } }); })(); In addition the script above is Syntactical incorrect The lines don't make sense. False is not a valid output you have to return a valid user ID:
Comments Hi Ralph, I removed each part in my code to find out which one make my code fail, and i do know that this line below is the reason:
But i do need to catch the Current UserID, so is there any other way?
PS: Don't care about my return value, it's just a test, i return a string to see the returned value on the web UI
Donald Nong
commented Jun 07 '16, 9:47 p.m.
If you look into the code more carefully, you will notice that the function getAuthenticatedContributor() is provided within com.ibm.team.repository.web.client.session. This is the whole reason why this method only works in a browser. As far as I know, this is the only way to get the logged-in user using JavaScript, with its obvious limitation.
|
Your answer
Dashboards and work items are no longer publicly available, so some links may be invalid. We now provide similar information through other means. Learn more here.
Comments
I converted your comment to a new question, as few people would notice it in such an old post.