Suren Konathala
Suren Konathala

Reputation: 3597

How to add Users and Roles to a Project created using AEM 6.2 Project API?

I'm trying to create Projects in AEM 6.2 using the new Project API [ https://docs.adobe.com/docs/en/aem/6-2/develop/ref/javadoc/com/adobe/cq/projects/api/Project.html ]

I am able to create the Project and also use a Template that i developed for that Project.

I'm creating the Project as:

 project = projectManager.createProject("/contents/projects",
                                        "Project1", 
                                        "Project Name", 
                                        "Creating a Test Project",
                                        "/apps/myproj/projects/templates/default");

When i am trying to add Users and Roles to the Project using the method:

 List<String> usersIds = ...
 List<String> rolesIds = ...
 project.updateMembers(userIds, roleIds);

I'm seeing the exceptions like: Caused by: com.adobe.cq.projects.impl.team.TeamException: The role role_editor is not available for admin.

I tried to map the values in lists 'usersIds' to 'rolesIds', like.. (userId)ksurendra --> (roleId) role_editor (This is another issue, maybe the API developers should have made it a Map instead of using two separate lists)

Appreciate any thoughts on this.

Upvotes: 1

Views: 751

Answers (1)

Suren Konathala
Suren Konathala

Reputation: 3597

After some research and testing, i found the solution.

The Solution is to add an Owner to the Project before any Users are assigned to the project.

So when you call

project.updateMembers(userIds, roleIds);

make sure the first values for the userIds and roleIds lists' are of the owner, and then the other users will follow.

For example:

 List<String> userIds = new ArrayList<String>{};
 userIds.add("suren");
 userIds.add("vassu");

 List<String> roleIds = new ArrayList<String>{};
 userIds.add("owner");
 userIds.add("designer");

Some background to the solution.

  1. When we create a Project using the API, we need to use a Template. And each of these Templates will have a pre-defined set of Roles. And these Roles will be assigned to the Project (see below).

enter image description here

  1. When we add a User to the above Project, we need to define the "Role" that user will have on the Project. This is the key thing.

  2. Every Project will have a Team, and a Project need to have an Owner before any other Users can be assigned.

References:

  1. Location for "default" templates is /libs/cq/core/content/projects/templates/default

  2. Project API here

  3. AEM 6.2 Projects

Upvotes: 0

Related Questions