work item customization: getting current user as logged in user
dojo.provide("testImport.setCurrentUser"); dojo.require("com.ibm.team.repository.web.client.session.Session");
(function() {
var WorkItemAttributes = com.ibm.team.workitem.api.common.WorkItemAttributes;
dojo.declare("testImport.setCurrentUser", null, {
getValue: function(attribute, workItem, configuration) {
var creator = workItem.getValue(WorkItemAttributes.CREATOR);
var loggedInUser = com.ibm.team.repository.web.client.session.getAuthenticatedContributor().itemId;
console.log("Set current user\ncreator:"+creator+ "\nloggedInUser:"+loggedInUser);
if(creator==loggedInUser){
return loggedInUser + "____This is login user";
}
return "False";
}
});
})();
2 answers
Comments
Thank Donald.
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.
For a default value (creation of the work item) create a default value. You can also try to depend your calculated value on the modification date, maybe that works better for you.
so basically your question is completely off. The script works, but not as you would expect. From that perspective, I would suggest to reword your question.
Hmm, from https://jazz.net/wiki/bin/view/Main/AttributeCustomization#Calculated_values
- When a work item is created, the script is executed
- When a work item is saved, the script is executed to recalculate the value.
- When an attribute which the current attribute depends on is changed, the value of the current attribute is recalculated.
Depending on the presentations of the two attributes this may not work in all cases.
Hi Ralph,
If you configure a Script-based calculated value, the script will be executed in three cases:
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:
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 ;
}
});
})();
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:
if(creator==loggedInUser){
return loggedInUser + "____This is login user";
}
return "False";
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:
getAuthenticatedContributor().userId
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.
Comments
Donald Nong
Jun 07 '16, 3:46 a.m.I converted your comment to a new question, as few people would notice it in such an old post.