Exploring Google Workspace Users with C# and Google APIs

If you’ve ever wondered how to interact with Google Workspace to manage and retrieve user information programmatically using C#, you’re in the right place. In this blog post, we’ll walk through a simple C# code snippet that utilizes the Google APIs to fetch and display information about users in a Google Workspace.


The code provided is a basic example that demonstrates how to use the Google APIs Client Library for .NET to interact with the Google Workspace Admin SDK. The Admin SDK allows you to manage Google Workspace domain resources, including user accounts.

Let’s break down the code step by step and understand what each part does.


  1. Create a service account key file
  2. Enable the Google calendar api
  3. Configure domain wide delegation to your service account from Google workspace.
  4. Include the Nuget Package Google.Apis.Admin.Directory.directory_v1

Setting Up the Environment

The first lines of code initialize the environment by setting up the necessary credentials and scopes. In this example, a service account is used to authenticate with the Google Workspace API. The workspaceAdmin variable specifies the email address of the user account on whose behalf the API calls will be made.

var scopes = new[] { DirectoryService.Scope.AdminDirectoryUser };
const string workspaceAdmin = "[Redacted]";
const string credentials = @"C:\workspaceserviceaccount.json";

Authenticating and Initializing the Service

The GoogleCredential.FromFile method is used to load the service account credentials from a JSON file. The CreateScoped method is then called to specify the desired scopes for the application. Finally, CreateWithUser associates the user account with the credential.

var credential = GoogleCredential.FromFile(credentials).CreateScoped(scopes).CreateWithUser(workspaceAdmin);
var services = new DirectoryService(new BaseClientService.Initializer()
    HttpClientInitializer = credential,

Making a Request

Now that the authentication and service initialization are complete, a request to the Google Workspace Admin SDK is created. The code specifies that it wants to list users (services.Users.List()), sets the customer parameter to “my_customer,” limits the results to 10 users, and orders the results by email.

var request = services.Users.List();
request.Customer = "my_customer";
request.MaxResults = 10;
request.OrderBy = UsersResource.ListRequest.OrderByEnum.Email;

Executing the Request and Processing Results

The request is then executed using request.Execute(), and the results are stored in the results variable. The code then checks if any users were returned. If not, it prints a message and exits. Otherwise, it iterates over the users and prints their email addresses and full names.

var results = request.Execute();
var users = results.UsersValue;

if (users.Count == 0)
    Console.WriteLine("No Users");

foreach (var user in users)
    Console.WriteLine($"{user.PrimaryEmail} ({user.Name.FullName})");


This blog post provided an overview of a C# code snippet that interacts with the Google Workspace Admin SDK to retrieve and display user information. This is just the tip of the iceberg; the Admin SDK offers a wide range of functionalities for managing various aspects of a Google Workspace domain.

Feel free to explore more features and customize the code to suit your specific needs. The official documentation for the Google Workspace Admin SDK is an excellent resource for further details and advanced usage.

Happy coding!

About Linda Lawton

My name is Linda Lawton I have more than 20 years experience working as an application developer and a database expert. I have also been working with Google APIs since 2012 and I have been contributing to the Google .Net client library since 2013. In 2013 I became a a Google Developer Experts for Google Analytics.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.