> ## Documentation Index
> Fetch the complete documentation index at: https://learn.social.plus/llms.txt
> Use this file to discover all available pages before exploring further.

# Query Community Members

> Query and search community members by membership status, roles, keyword, and sort order

Use member query APIs to list community members and member search APIs to find members by keyword. The available filters and sort values differ by SDK, so use the platform-specific values below.

<Info>
  Member query APIs return paginated or live collection results depending on the platform. Use pagination controls for large communities.
</Info>

## Parameters

### Common Parameters

| Parameter                  | Description                                                                           |
| -------------------------- | ------------------------------------------------------------------------------------- |
| `communityId`              | Community to query.                                                                   |
| `roles`                    | Optional role IDs to include, such as `community-moderator`.                          |
| `includeDeleted`           | Include deleted users when supported.                                                 |
| `excludingRoles`           | Exclude members with any role in the list. Available in TypeScript, iOS, and Android. |
| `limit` / pagination token | Page-size and pagination controls where supported.                                    |

### Query Filters

| Platform   | Values                                                                         |
| ---------- | ------------------------------------------------------------------------------ |
| TypeScript | `memberships: ["member"]`, `["banned"]`, or omit `memberships` for all results |
| iOS        | `AmityCommunityMembership.QueryFilter.member`, `.banned`, `.all`               |
| Android    | `AmityCommunityMembershipFilter.MEMBER`, `BANNED`, `ALL`                       |
| Flutter    | `AmityCommunityMembershipFilter.MEMBER`, `BANNED`, `ALL`                       |

### Query Sort Options

| Platform   | Values                                                       |
| ---------- | ------------------------------------------------------------ |
| TypeScript | `"firstCreated"`, `"lastCreated"`                            |
| iOS        | `.displayName`, `.firstCreated`, `.lastCreated`, `.lastJoin` |
| Android    | `DISPLAY_NAME`, `FIRST_CREATED`, `LAST_CREATED`, `LAST_JOIN` |
| Flutter    | `FIRST_CREATED`, `LAST_CREATED`                              |

Query members when you need a paginated member list with membership, role, deletion, and sort filters.

<CodeGroup>
  ```swift iOS theme={null}
  let communityMembership = AmityCommunityMembership(communityId: communityId)

  let liveCollection = communityMembership.getMembers(
      filter: .member,
      roles: ["community-moderator"],
      sortBy: .firstCreated,
      includeDeleted: false,
      excludingRoles: ["channel-moderator"]
  )

  token = liveCollection.observe { collection, error in
      if let error = error {
          handleError(error)
          return
      }

      let members = collection.snapshots
      // Render members
  }
  ```

  ```kotlin Android theme={null}
  fun queryCommunityMembers(communityRepository: AmityCommunityRepository) {
      communityRepository
          .membership(communityId = "communityId")
          .getMembers()
          .filter(filter = AmityCommunityMembershipFilter.MEMBER)
          .roles(roles = listOf("community-moderator"))
          .excludingRoles(excludingRoles = listOf("channel-moderator"))
          .includeDeleted(includeDeleted = false)
          .sortBy(sortBy = AmityCommunityMembershipSortOption.FIRST_CREATED)
          .build()
          .query()
          .doOnNext { members: PagingData<AmityCommunityMember> ->
              // Render members
          }
          .doOnError { throwable ->
              // Handle error
          }
          .subscribe()
  }
  ```

  ```typescript TypeScript theme={null}
  import { CommunityRepository } from '@amityco/ts-sdk';

  let nextPageFn: (() => void) | undefined;
  let hasMore = false;

  const unsubscriber = CommunityRepository.Membership.getMembers(
    {
      communityId,
      memberships: ['member'],
      roles: ['community-moderator'],
      excludingRoles: ['channel-moderator'],
      sortBy: 'firstCreated',
      includeDeleted: false,
      limit: 20,
    },
    ({ data: members, onNextPage, hasNextPage, loading, error }) => {
      if (error) {
        // Handle error
        return;
      }

      if (!loading && members) {
        // Render members
      }

      hasMore = hasNextPage;
      nextPageFn = onNextPage;
    },
  );

  function loadMoreMembers() {
    if (hasMore) nextPageFn?.();
  }
  ```

  ```dart Flutter theme={null}
  final members = <AmityCommunityMember>[];
  late PagingController<AmityCommunityMember> membersController;

  void queryCommunityMembers(String communityId) {
    membersController = PagingController(
      pageFuture: (token) => AmitySocialClient.newCommunityRepository()
          .membership(communityId)
          .getMembers()
          .filter(AmityCommunityMembershipFilter.MEMBER)
          .roles(['community-moderator'])
          .includeDeleted(false)
          .sortBy(AmityCommunityMembershipSortOption.FIRST_CREATED)
          .getPagingData(token: token, limit: 20),
      pageSize: 20,
    )..addListener(() {
        if (membersController.error == null) {
          members
            ..clear()
            ..addAll(membersController.loadedItems);
        } else {
          // Handle pagination error
        }
      });
  }
  ```
</CodeGroup>

## Search Community Members

Search APIs use the same community and role controls, plus a keyword/display-name search input.

### Search Filters

| Platform   | Values                                                         |
| ---------- | -------------------------------------------------------------- |
| TypeScript | `memberships: ["member"]`, `["banned"]`, or omit `memberships` |
| iOS        | `AmityCommunityMembership.SearchFilter.member`, `.banned`      |
| Android    | `AmityCommunityMembership.MEMBER`, `BANNED`                    |
| Flutter    | `AmityCommunityMembershipFilter.MEMBER`, `BANNED`, `ALL`       |

### Search Sort Options

| Platform   | Values                                                       |
| ---------- | ------------------------------------------------------------ |
| TypeScript | `"displayName"`, `"firstCreated"`, `"lastCreated"`           |
| iOS        | `.displayName`, `.firstCreated`, `.lastCreated`, `.lastJoin` |
| Android    | `DISPLAY_NAME`, `FIRST_CREATED`, `LAST_CREATED`, `LAST_JOIN` |
| Flutter    | `FIRST_CREATED`, `LAST_CREATED`                              |

<CodeGroup>
  ```swift iOS theme={null}
  let communityMembership = AmityCommunityMembership(communityId: communityId)

  let liveCollection = communityMembership.searchMembers(
      keyword: "alex",
      filter: [.member],
      roles: ["community-moderator"],
      sortBy: .displayName,
      includeDeleted: false,
      excludingRoles: ["channel-moderator"]
  )

  token = liveCollection.observe { collection, error in
      if let error = error {
          handleError(error)
          return
      }

      let members = collection.snapshots
      // Render matching members
  }
  ```

  ```kotlin Android theme={null}
  fun searchCommunityMembers(
      communityRepository: AmityCommunityRepository,
      keyword: String
  ) {
      communityRepository
          .membership(communityId = "communityId")
          .searchMembers(keyword = keyword)
          .roles(roles = listOf("community-moderator"))
          .membershipFilter(
              communityMembership = listOf(AmityCommunityMembership.MEMBER)
          )
          .excludingRoles(excludingRoles = listOf("channel-moderator"))
          .includeDeleted(includeDeleted = false)
          .sortBy(AmityCommunityMembershipSortOption.DISPLAY_NAME)
          .build()
          .query()
          .doOnNext { members: PagingData<AmityCommunityMember> ->
              // Render matching members
          }
          .subscribe()
  }
  ```

  ```typescript TypeScript theme={null}
  import { CommunityRepository } from '@amityco/ts-sdk';

  function searchCommunityMembers(keyword: string) {
    return CommunityRepository.Membership.searchMembers(
      {
        communityId,
        search: keyword,
        memberships: ['member'],
        roles: ['community-moderator'],
        excludingRoles: ['channel-moderator'],
        sortBy: 'displayName',
        includeDeleted: false,
        limit: 20,
      },
      ({ data: members, loading, error }) => {
        if (error) {
          // Handle error
          return;
        }

        if (!loading && members) {
          // Render matching members
        }
      },
    );
  }
  ```

  ```dart Flutter theme={null}
  final searchResults = <AmityCommunityMember>[];
  late PagingController<AmityCommunityMember> searchController;

  void searchCommunityMembers(String communityId, String keyword) {
    searchController = PagingController(
      pageFuture: (token) => AmitySocialClient.newCommunityRepository()
          .membership(communityId)
          .searchMembers(keyword)
          .filter(AmityCommunityMembershipFilter.MEMBER)
          .roles(['community-moderator'])
          .includeDeleted(false)
          .sortBy(AmityCommunityMembershipSortOption.FIRST_CREATED)
          .getPagingData(token: token, limit: 20),
      pageSize: 20,
    )..addListener(() {
        if (searchController.error == null) {
          searchResults
            ..clear()
            ..addAll(searchController.loadedItems);
        } else {
          // Handle pagination error
        }
      });
  }
  ```
</CodeGroup>

## Excluding Roles

Use `excludingRoles` when you need a member list that removes users with any role in the exclusion list. This filter is available in TypeScript, iOS, and Android. The current Flutter member query builder does not expose `excludingRoles`.

<CodeGroup>
  ```typescript TypeScript theme={null}
  import { CommunityRepository } from '@amityco/ts-sdk';

  const unsubscriber = CommunityRepository.Membership.getMembers(
    {
      communityId,
      excludingRoles: ['community-moderator', 'channel-moderator'],
      limit: 20,
    },
    ({ data: members }) => {
      // Members do not include users with excluded roles
    },
  );
  ```

  ```swift iOS theme={null}
  let communityMembership = AmityCommunityMembership(communityId: communityId)

  let liveCollection = communityMembership.getMembers(
      filter: .member,
      roles: [],
      sortBy: .firstCreated,
      includeDeleted: false,
      excludingRoles: ["community-moderator", "channel-moderator"]
  )

  token = liveCollection.observe { collection, error in
      let members = collection.snapshots
      // Members do not include users with excluded roles
  }
  ```

  ```kotlin Android theme={null}
  fun queryMembersExcludingRoles(communityRepository: AmityCommunityRepository) {
      communityRepository
          .membership(communityId = "communityId")
          .getMembers()
          .filter(filter = AmityCommunityMembershipFilter.MEMBER)
          .excludingRoles(
              excludingRoles = listOf("community-moderator", "channel-moderator")
          )
          .build()
          .query()
          .doOnNext { members: PagingData<AmityCommunityMember> ->
              // Members do not include users with excluded roles
          }
          .subscribe()
  }
  ```
</CodeGroup>

## Related Topics

<CardGroup cols={2}>
  <Card icon="user-plus" href="./join-leave-community" title="Join / Leave Community">
    Manage joining and leaving communities.
  </Card>

  <Card icon="users-cog" href="./member-management" title="Member Management">
    Add and remove community members.
  </Card>

  <Card icon="shield-check" href="./community-moderation" title="Community Moderation">
    Manage roles, bans, and permissions.
  </Card>

  <Card icon="key" href="/social-plus-sdk/core-concepts/user-management/roles-permissions" title="Roles & Permissions">
    Understand role-based access control.
  </Card>
</CardGroup>
