# Recuperate organisation groups and roles

**URL:** <https://community.ofelia.com/t/recuperate-organisation-groups-and-roles/4823>\
**Category:** Q&A\
**Created:** [4 July 2014 10:39 UTC](https://community.ofelia.com/t/recuperate-organisation-groups-and-roles/4823 "2014-07-04T10:39:59Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![rahmi.hichem](https://avatars.discourse-cdn.com/v4/letter/r/e0b2c6/32.png) [@rahmi.hichem](https://community.ofelia.com/u/rahmi.hichem)\
**Post date:** [4 July 2014 10:39 UTC](https://community.ofelia.com/t/recuperate-organisation-groups-and-roles/4823/1 "2014-07-04T10:39:59Z")

</div>

I want to recuperate groups and roles of my organisation in two combobox, of corse with a Groovy code.

Any ideas ?

---

<div class="post-metadata">

**Author:** ![yannick.lombardi](https://avatars.discourse-cdn.com/v4/letter/y/8e8cbc/32.png) [@yannick.lombardi](https://community.ofelia.com/u/yannick.lombardi)\
**Post date:** [4 July 2014 12:00 UTC](https://community.ofelia.com/t/recuperate-organisation-groups-and-roles/4823/2 "2014-07-04T12:00:14Z")

</div>

Hi.

You need to create 2 groovy code :  
  
import org.bonitasoft.engine.identity.GroupCriterion;  
import org.bonitasoft.engine.identity.Group;  
List list = new ArrayList();  
List groupList = apiAccessor.getIdentityAPI().getGroups(startIndex, maxResults, GroupCriterion.NAME\_ASC);  
for (Group group : groupList) {  
list.add(group.getName());  
}  
return list;  
  
and  
  
import org.bonitasoft.engine.identity.RoleCriterion;  
import org.bonitasoft.engine.identity.Role;  
List list = new ArrayList();  
List roleList = apiAccessor.getIdentityAPI().getRoles(startIndex, maxResults, RoleCriterion.NAME\_ASC);  
for (Role role : roleList) {  
list.add(role.getName());  
}  
return list;

---

<div class="post-metadata">

**Author:** ![rahmi.hichem](https://avatars.discourse-cdn.com/v4/letter/r/e0b2c6/32.png) [@rahmi.hichem](https://community.ofelia.com/u/rahmi.hichem)\
**Post date:** [4 July 2014 15:50 UTC](https://community.ofelia.com/t/recuperate-organisation-groups-and-roles/4823/3 "2014-07-04T15:50:39Z")

</div>

Thank you. Just i have a litle problem. For maxResults i did this :

long nb = apiAccessor.getIdentityAPI().getNumberOfGroups();

But i get an empty list !

---

<div class="post-metadata">

**Author:** ![sylvain.bailly](https://avatars.discourse-cdn.com/v4/letter/s/f17d59/32.png) [@sylvain.bailly](https://community.ofelia.com/u/sylvain.bailly)\
**Post date:** [7 July 2014 12:25 UTC](https://community.ofelia.com/t/recuperate-organisation-groups-and-roles/4823/4 "2014-07-07T12:25:01Z")

</div>

This works:

import org.bonitasoft.engine.identity.GroupCriterion;  
import org.bonitasoft.engine.identity.Group;

int nb = apiAccessor.getIdentityAPI().getNumberOfGroups();  
List list = new ArrayList();  
List groupList = apiAccessor.getIdentityAPI().getGroups(0, nb, GroupCriterion.NAME\_ASC);

for (Group group : groupList) {  
list.add(group.getName());  
}  
return list;

You can download a test process here (zip file): [http://dl.free.fr/getfile.pl?file=/RsZTpw4o](http://dl.free.fr/getfile.pl?file=/RsZTpw4o)

---

<div class="post-metadata">

**Author:** ![rahmi.hichem](https://avatars.discourse-cdn.com/v4/letter/r/e0b2c6/32.png) [@rahmi.hichem](https://community.ofelia.com/u/rahmi.hichem)\
**Post date:** [7 July 2014 11:16 UTC](https://community.ofelia.com/t/recuperate-organisation-groups-and-roles/4823/5 "2014-07-07T11:16:18Z")

</div>

I get an empty list ! May you give me more details about how to implement the code ?

---

<div class="post-metadata">

**Author:** ![yannick.lombardi](https://avatars.discourse-cdn.com/v4/letter/y/8e8cbc/32.png) [@yannick.lombardi](https://community.ofelia.com/u/yannick.lombardi)\
**Post date:** [7 July 2014 12:08 UTC](https://community.ofelia.com/t/recuperate-organisation-groups-and-roles/4823/6 "2014-07-07T12:08:45Z")

</div>

Hi.

In the pool, I add 2 data : groupList and roleList (of type List).  
I create a Script Task. In the Operation tab of this task, I add 2 scripts.  
The first script : “groupList” Takes value of “groupScript”.  
Same thing for roleList with “roleScript”.  
In the script I add the code.

I try to use : long nb = apiAccessor.getIdentityAPI().getNumberOfGroups();

You can do for example (groupScript) :  
  
import org.bonitasoft.engine.identity.GroupCriterion;  
import org.bonitasoft.engine.identity.Group;  
List list = new ArrayList();  
List groupList = apiAccessor.getIdentityAPI().getGroups(0, (int) apiAccessor.getIdentityAPI().getNumberOfGroups(), GroupCriterion.NAME\_ASC);  
for (Group group : groupList) {  
list.add(group.getName());  
}  
return list;

---

<div class="post-metadata">

**Author:** ![rahmi.hichem](https://avatars.discourse-cdn.com/v4/letter/r/e0b2c6/32.png) [@rahmi.hichem](https://community.ofelia.com/u/rahmi.hichem)\
**Post date:** [7 July 2014 14:18 UTC](https://community.ofelia.com/t/recuperate-organisation-groups-and-roles/4823/7 "2014-07-07T14:18:22Z")

</div>

It works 😉 .

Do you have any idea about how to assign selected groupe and role to the user ? in the same code of corse

---

<div class="post-metadata">

**Author:** ![yannick.lombardi](https://avatars.discourse-cdn.com/v4/letter/y/8e8cbc/32.png) [@yannick.lombardi](https://community.ofelia.com/u/yannick.lombardi)\
**Post date:** [7 July 2014 14:32 UTC](https://community.ofelia.com/t/recuperate-organisation-groups-and-roles/4823/8 "2014-07-07T14:32:00Z")

</div>

To assign a Group and Role to a user, you can use the function addUserMembership() :  
  
import org.bonitasoft.engine.identity.Group;  
import org.bonitasoft.engine.identity.Role;  
import org.bonitasoft.engine.identity.User;  
User user = apiAccessor.getIdentityAPI().getUserByUserName(userName);  
Role role = apiAccessor.getIdentityAPI().getRoleByName(roleName);  
Group group = apiAccessor.getIdentityAPI().getGroupByPath(groupName);  
apiAccessor.getIdentityAPI().addUserMembership(user.getId(), group.getId(), role.getId());

You need to create a script that return nothing and assign it to a empty variable.  
For exemple I do this in a Scritp Task:  
“emptyVariable” take value of “assignScript”  
with this script :  
  
import org.bonitasoft.engine.identity.Group;  
import org.bonitasoft.engine.identity.Role;  
import org.bonitasoft.engine.identity.User;

apiAccessor.getIdentityAPI().createGroup(“NewGroup”, “”);  
apiAccessor.getIdentityAPI().createRole(“NewRole”);

User user = apiAccessor.getIdentityAPI().getUserByUserName(“YLombardi”);  
Role role = apiAccessor.getIdentityAPI().getRoleByName(“NewRole”);  
Group group = apiAccessor.getIdentityAPI().getGroupByPath(“NewGroup”);  
apiAccessor.getIdentityAPI().addUserMembership(user.getId(), group.getId(), role.getId());

return “nothing”;

---

<div class="post-metadata">

**Author:** ![rahmi.hichem](https://avatars.discourse-cdn.com/v4/letter/r/e0b2c6/32.png) [@rahmi.hichem](https://community.ofelia.com/u/rahmi.hichem)\
**Post date:** [7 July 2014 14:55 UTC](https://community.ofelia.com/t/recuperate-organisation-groups-and-roles/4823/9 "2014-07-07T14:55:24Z")

</div>

By UserName you mean the login ? Because i want to do that just after this code :  
  
import org.bonitasoft.engine.identity.ContactDataCreator;  
import org.bonitasoft.engine.identity.UserCreator;

ContactDataCreator cd = new ContactDataCreator();  
cd.setEmail(field\_rec\_mail1);  
UserCreator user = new UserCreator (field\_u\_login3, field\_u\_pw3);  
user.setPersonalContactData(cd);  
user.setFirstName(field\_f\_name3).setLastName(field\_p\_name3);  
apiAccessor.getIdentityAPI().createUser(user);

---

<div class="post-metadata">

**Author:** ![yannick.lombardi](https://avatars.discourse-cdn.com/v4/letter/y/8e8cbc/32.png) [@yannick.lombardi](https://community.ofelia.com/u/yannick.lombardi)\
**Post date:** [7 July 2014 15:03 UTC](https://community.ofelia.com/t/recuperate-organisation-groups-and-roles/4823/10 "2014-07-07T15:03:16Z")

</div>

Yes, the userName is the login of the user.

If you want to add this just after your code, I think you can do this :

import org.bonitasoft.engine.identity.ContactDataCreator;  
import org.bonitasoft.engine.identity.UserCreator;  
import org.bonitasoft.engine.identity.Group;  
import org.bonitasoft.engine.identity.Role;  
import org.bonitasoft.engine.identity.User;

ContactDataCreator cd = new ContactDataCreator();  
cd.setEmail(field\_rec\_mail1);  
UserCreator userCreator = new UserCreator (field\_u\_login3, field\_u\_pw3);  
userCreator.setPersonalContactData(cd);  
userCreator.setFirstName(field\_f\_name3).setLastName(field\_p\_name3);  
User user = apiAccessor.getIdentityAPI().createUser(userCreator);

Role role = apiAccessor.getIdentityAPI().getRoleByName(roleName);  
Group group = apiAccessor.getIdentityAPI().getGroupByPath(groupName);  
apiAccessor.getIdentityAPI().addUserMembership(user.getId(), group.getId(), role.getId());

---

<div class="post-metadata">

**Author:** ![rahmi.hichem](https://avatars.discourse-cdn.com/v4/letter/r/e0b2c6/32.png) [@rahmi.hichem](https://community.ofelia.com/u/rahmi.hichem)\
**Post date:** [7 July 2014 15:16 UTC](https://community.ofelia.com/t/recuperate-organisation-groups-and-roles/4823/11 "2014-07-07T15:16:18Z")

</div>

I tried this befor but i get an “Error when submitting the form” ! it should works if we follow the logic but it doesn’t 😕

---

<div class="post-metadata">

**Author:** ![yannick.lombardi](https://avatars.discourse-cdn.com/v4/letter/y/8e8cbc/32.png) [@yannick.lombardi](https://community.ofelia.com/u/yannick.lombardi)\
**Post date:** [8 July 2014 08:15 UTC](https://community.ofelia.com/t/recuperate-organisation-groups-and-roles/4823/12 "2014-07-08T08:15:53Z")

</div>

I try it and it works.  
Here is my code :  
  
import org.bonitasoft.engine.identity.ContactDataCreator;  
import org.bonitasoft.engine.identity.UserCreator;  
import org.bonitasoft.engine.identity.Group;  
import org.bonitasoft.engine.identity.Role;  
import org.bonitasoft.engine.identity.User;

apiAccessor.getIdentityAPI().createGroup(“NewGroup”, “”);  
apiAccessor.getIdentityAPI().createRole(“NewRole”);

ContactDataCreator cd = new ContactDataCreator();  
cd.setEmail(“[adresse@mail.fr](mailto:adresse@mail.fr)”);  
UserCreator userCreator = new UserCreator (“login”, “password”);  
userCreator.setPersonalContactData(cd);  
userCreator.setFirstName(“FirstName”).setLastName(“LastName”);  
User user = apiAccessor.getIdentityAPI().createUser(userCreator);

Role role = apiAccessor.getIdentityAPI().getRoleByName(“NewRole”);  
Group group = apiAccessor.getIdentityAPI().getGroupByPath(“NewGroup”);  
apiAccessor.getIdentityAPI().addUserMembership(user.getId(), group.getId(), role.getId());

---

<div class="post-metadata">

**Author:** ![rahmi.hichem](https://avatars.discourse-cdn.com/v4/letter/r/e0b2c6/32.png) [@rahmi.hichem](https://community.ofelia.com/u/rahmi.hichem)\
**Post date:** [8 July 2014 08:25 UTC](https://community.ofelia.com/t/recuperate-organisation-groups-and-roles/4823/13 "2014-07-08T08:25:31Z")

</div>

In the first lines of code :  
  
apiAccessor.getIdentityAPI().createGroup(“NewGroup”, “”);  
apiAccessor.getIdentityAPI().createRole(“NewRole”);

You mean that in “NewGroup” and “NewRole” i should put my my selected items in the form coresponding to my groupe and role ?

And what about the second ones (“NewGroup” and “NewRole”) should i put the same thing ? because i dont understand why you created them if they already exist ? (I mean in the first tow lines of code) ? and why (“NewGroup”,“”) when u create groupe why not just (“NewGroup”);

Thanks

---

<div class="post-metadata">

**Author:** ![yannick.lombardi](https://avatars.discourse-cdn.com/v4/letter/y/8e8cbc/32.png) [@yannick.lombardi](https://community.ofelia.com/u/yannick.lombardi)\
**Post date:** [8 July 2014 08:40 UTC](https://community.ofelia.com/t/recuperate-organisation-groups-and-roles/4823/14 "2014-07-08T08:40:08Z")

</div>

No, I create a group and a role for the test. You don’t need to create them if they already exist.

You should delete lines 7-8 and just get your existing group/role with line 17-18.

Why (“NewGroup”,“”) : because bonitasoft create the function like this. The function to create a group is createGroup(groupName, parentGroupName);  
I have no parent group so I keep it empty.

---

<div class="post-metadata">

**Author:** ![rahmi.hichem](https://avatars.discourse-cdn.com/v4/letter/r/e0b2c6/32.png) [@rahmi.hichem](https://community.ofelia.com/u/rahmi.hichem)\
**Post date:** [8 July 2014 08:49 UTC](https://community.ofelia.com/t/recuperate-organisation-groups-and-roles/4823/15 "2014-07-08T08:49:15Z")

</div>

Here is what i put :

import org.bonitasoft.engine.identity.ContactDataCreator; import org.bonitasoft.engine.identity.UserCreator; import org.bonitasoft.engine.profile.ProfileMemberCreator; import org.bonitasoft.engine.identity.Group; import org.bonitasoft.engine.identity.Role; import org.bonitasoft.engine.identity.User;

ContactDataCreator cd = new ContactDataCreator();  
cd.setEmail(field\_rec\_mail1);  
UserCreator userCreator = new UserCreator (field\_u\_login3, field\_u\_pw3);  
userCreator.setPersonalContactData(cd);

userCreator.setFirstName(field\_f\_name3).setLastName(field\_p\_name3);

User user = apiAccessor.getIdentityAPI().createUser(userCreator);

Group group = apiAccessor.getIdentityAPI().getGroupByPath(field\_Sélectionner1);  
Role role = apiAccessor.getIdentityAPI().getRoleByName(field\_Sélectionner2);  
apiAccessor.getIdentityAPI().addUserMembership(user.getId(), group.getId(), role.getId());

You said that u did the same thing and it works for you ?

---

<div class="post-metadata">

**Author:** ![yannick.lombardi](https://avatars.discourse-cdn.com/v4/letter/y/8e8cbc/32.png) [@yannick.lombardi](https://community.ofelia.com/u/yannick.lombardi)\
**Post date:** [8 July 2014 08:52 UTC](https://community.ofelia.com/t/recuperate-organisation-groups-and-roles/4823/16 "2014-07-08T08:52:35Z")

</div>

Yes, I do this and it works.

You can see the new User on Bonita Portal. You need to use a Administrator Profil and look in the Organisation tab.

---

<div class="post-metadata">

**Author:** ![rahmi.hichem](https://avatars.discourse-cdn.com/v4/letter/r/e0b2c6/32.png) [@rahmi.hichem](https://community.ofelia.com/u/rahmi.hichem)\
**Post date:** [8 July 2014 08:59 UTC](https://community.ofelia.com/t/recuperate-organisation-groups-and-roles/4823/17 "2014-07-08T08:59:01Z")

</div>

Yes of corse . But the problem is that i get an “error while submitting the form” . I will look for the problem !

By the way, is there any solution to attribute the profile (Administrator or user) by some groovy code or i must do it in the portal ?

For information i am using the community version

---

<div class="post-metadata">

**Author:** ![yannick.lombardi](https://avatars.discourse-cdn.com/v4/letter/y/8e8cbc/32.png) [@yannick.lombardi](https://community.ofelia.com/u/yannick.lombardi)\
**Post date:** [8 July 2014 09:05 UTC](https://community.ofelia.com/t/recuperate-organisation-groups-and-roles/4823/18 "2014-07-08T09:05:19Z")

</div>

I don’t know why you have this error.  
What says the log file ?  
Your data (field\_rec\_mail1, field\_u\_login3, …) are not null ?

For the profil, maybe you can do that with the ProfilAPI but I havn’t try this yet. I always use the portal.

---

<div class="post-metadata">

**Author:** ![sylvain.bailly](https://avatars.discourse-cdn.com/v4/letter/s/f17d59/32.png) [@sylvain.bailly](https://community.ofelia.com/u/sylvain.bailly)\
**Post date:** [9 July 2014 12:26 UTC](https://community.ofelia.com/t/recuperate-organisation-groups-and-roles/4823/19 "2014-07-09T12:26:29Z")

</div>

The solution is to use  
  
apiAccessor.getProfileAPI().createProfileMember(1, u.getId(), g.getId(), r.getId())

1 is for User and 2 for Admin

---

<div class="post-metadata">

**Author:** ![sylvain.bailly](https://avatars.discourse-cdn.com/v4/letter/s/f17d59/32.png) [@sylvain.bailly](https://community.ofelia.com/u/sylvain.bailly)\
**Post date:** [9 July 2014 12:26 UTC](https://community.ofelia.com/t/recuperate-organisation-groups-and-roles/4823/20 "2014-07-09T12:26:30Z")

</div>

The solution is to use  
  
apiAccessor.getProfileAPI().createProfileMember(1, u.getId(), g.getId(), r.getId())

1 is for User and 2 for Admin

[Next page](https://community.ofelia.com/t/recuperate-organisation-groups-and-roles/4823.md?page=2)
