Listing All People and Groups in a Group - Alfresco Content Services - 23.4 - 23.4 - Ready - Alfresco - external

Alfresco Content Services

Platform
Alfresco
Product
Alfresco Content Services
Release
23.4
License

Listing all the members of a group (i.e. people and groups) uses the listGroupMemberships method of the GroupsApi.

For more information about this ReST API endpoint, see List All People and Groups in a Group.

For a description of the common parameters, such as fields, see Common Parameters.

import org.alfresco.core.handler.GroupsApi;
import org.alfresco.core.model.GroupMemberEntry;
import org.alfresco.core.model.GroupMemberPaging;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.List;

@Component
public class ListGroupMembersCmd {
    static final Logger LOGGER = LoggerFactory.getLogger(ListGroupMembersCmd.class);

    @Autowired
    GroupsApi groupsApi;

    public void execute(String groupId) throws IOException {
        Integer skipCount = 0;
        Integer maxItems = 100;
        String where = null;
        List<String> orderBy = null;
        List<String> fields = null;

        LOGGER.info("Listing members of group {}:", groupId);
        GroupMemberPaging groupMembers = groupsApi.listGroupMemberships(
                groupId, skipCount, maxItems, orderBy, where, fields).getBody();
        for (GroupMemberEntry groupMemberEntry: groupMembers.getList().getEntries()) {
            LOGGER.info("  {} ({})", groupMemberEntry.getEntry().getDisplayName(), 
                    groupMemberEntry.getEntry().getMemberType());
        }
    }
}

Executing this code will list the members of passed in group id, note that you have to prefix group ids with GROUP_:

% java -jar target/rest-api-0.0.1-SNAPSHOT.jar list-group-members GROUP_engineering

2021-05-06 12:55:43.231  INFO 26500 --- [           main] o.a.tutorial.restapi.RestApiApplication  : Started RestApiApplication in 3.59 seconds (JVM running for 4.024)
2021-05-06 12:55:43.233  INFO 26500 --- [           main] o.a.tutorial.restapi.RestApiApplication  : args[0]: list-group-members
2021-05-06 12:55:43.234  INFO 26500 --- [           main] o.a.tutorial.restapi.RestApiApplication  : args[1]: GROUP_engineering
2021-05-06 12:55:43.234  INFO 26500 --- [           main] o.a.t.restapi.ListGroupMembersCmd        : Listing members of group GROUP_engineering:
2021-05-06 12:55:43.404  INFO 26500 --- [           main] o.a.t.restapi.ListGroupMembersCmd        :   martin (PERSON)
2021-05-06 12:55:43.404  INFO 26500 --- [           main] o.a.t.restapi.ListGroupMembersCmd        :   System Architects (GROUP)