List tenants using client.

## Explore further


For detailed documentation that includes this code sample, see the following:

- [Tenant basics](https://docs.cloud.google.com/talent-solution/job-search/docs/tenants.md.txt)
- [Tenant basics (v4beta1)](https://docs.cloud.google.com/talent-solution/job-search/v4beta1/docs/tenants.md.txt)

## Code sample

### Java


To learn how to install and use the client library for CTS, see
[CTS client libraries](https://docs.cloud.google.com/talent-solution/job-search/docs/libraries.md.txt).


For more information, see the
[CTS Java API
reference documentation](https://docs.cloud.google.com/java/docs/reference/google-cloud-talent/latest/overview).


To authenticate to CTS, set up Application Default Credentials.
For more information, see

[Set up authentication for a local development environment](https://docs.cloud.google.com/docs/authentication/set-up-adc-local-dev-environment.md.txt).

```java

import com.google.cloud.talent.v4beta1.ListTenantsRequest;
import com.google.cloud.talent.v4beta1.ProjectName;
import com.google.cloud.talent.v4beta1.Tenant;
import com.google.cloud.talent.v4beta1.TenantServiceClient;
import java.io.IOException;

public class JobSearchListTenants {

  public static void listTenants() throws IOException {
    // TODO(developer): Replace these variables before running the sample.
    String projectId = "your-project-id";
    listTenants(projectId);
  }

  // List Tenants.
  public static void listTenants(String projectId) throws IOException {
    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests. After completing all of your requests, call
    // the "close" method on the client to safely clean up any remaining background resources.
    try (TenantServiceClient tenantServiceClient = TenantServiceClient.create()) {
      ProjectName parent = ProjectName.of(projectId);

      ListTenantsRequest request =
          ListTenantsRequest.newBuilder().setParent(parent.toString()).build();

      for (Tenant responseItem : tenantServiceClient.listTenants(request).iterateAll()) {
        System.out.format("Tenant Name: %s%n", responseItem.getName());
        System.out.format("External ID: %s%n", responseItem.getExternalId());
      }
    }
  }
}
```

## What's next


To search and filter code samples for other Google Cloud products, see the
[Google Cloud sample browser](https://docs.cloud.google.com/docs/samples.md.txt?product=talentsolution).