work item customization: getting current user as logged in user
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");
(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
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
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:
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
Donald Nong
Jun 07 '16, 3:46 a.m.