How to get all mail adresses of one Role

Hello,

I want to get all users mail adresses, those users belong to a specific role called “Administrator” . Any ideas ?

Thank you in advance

Hello,

here is a script that you can use to retrieve emails of all users from the default role “member”. You will have to adjust it for your use case and make sure that you find your role and its id. It returns a list of emails.

import org.bonitasoft.engine.identity.ContactData; import org.bonitasoft.engine.identity.User; import org.bonitasoft.engine.identity.UserCriterion; import com.bonitasoft.engine.api.IdentityAPI;

List listEmail = new ArrayList();
IdentityAPI identityAPI = apiAccessor.getIdentityAPI();

List listUsers = identityAPI.getUsersInRole(1, 0, 20, UserCriterion.USER_NAME_ASC); //1 is the role id and 20 is the number of results to return - adjust for your needs
for (User user : listUsers) {
ContactData cd = identityAPI.getUserContactData(user.getId(), false);
listEmail.add(cd.getEmail())
}
return listEmail

Hope this helps,
Haris

Hi,

To get the personal & professional email :

import org.bonitasoft.engine.identity.ContactData;
import org.bonitasoft.engine.identity.User;
import org.bonitasoft.engine.identity.UserCriterion;
import org.bonitasoft.engine.identity.Role;
import com.bonitasoft.engine.api.IdentityAPI;

final IdentityAPI identityAPI = apiAccessor.getIdentityAPI();
final Role role = identityAPI.getRoleByName(“Administrator”);

final List listEmail = new ArrayList();

List listUsers = identityAPI.getUsersInRole(role.getId(), 0, 20, UserCriterion.USER_NAME_ASC);
for (User user : listUsers) {
// To have professional email
ContactData professionalData = identityAPI.getUserContactData(user.getId(), false);
String professionalEmail = professionalData.getEmail();
if (professionalEmail != null && !professionalEmail.isEmpty()) {
listEmail.add(professionalEmail);
}

// To have personal email
ContactData personalData = identityAPI.getUserContactData(user.getId(), true);
String personalEmail = personalData.getEmail();
if (personalEmail != null && !personalEmail.isEmpty()) {
	listEmail.add(personalEmail);
}

}
return listEmail

To have a variable of the type List, you need to select String and Multiple when you create this.

Thank you for your answer. It works :slight_smile: . But i have a little problem, when i try to assign the code to my variable that contains all mail adresses (with the code that you gave to me), i have to cast it to string in the connector because he dont accept Java.util.List type as variable, how can i do it ?

Hi,

To get your role :

import org.bonitasoft.engine.identity.Role;
import com.bonitasoft.engine.api.IdentityAPI;

final Role role = identityAPI.getRoleByName(“Administrator”);

Best regards,
Céline

Thanks both of you,

Actualy i have an other code and it works, the problem was that when i create a new administrator, i add his mail adress only in personnel data and not in professionel data to, so the next time when i create a new user and i want to get all mail adresses my code can’t get the new mail adress because he don’t find it (it was added only in personnel data), and here was the problem.

Thank you again.
Hichem