/// <summary>
        /// Run the code example.
        /// </summary>
        /// <param name="user">The DFP user object running the code example.</param>
        public override void Run(DfpUser user)
        {
            // Get the UserService.
              UserService userService = (UserService) user.GetService(DfpService.v201208.UserService);

              // Sets defaults for page and Statement.
              UserPage page = new UserPage();
              Statement statement = new Statement();
              int offset = 0;

              try {
            do {
              // Create a Statement to get all users.
              statement.query = string.Format("LIMIT 500 OFFSET {0}", offset);

              // Get users by Statement.
              page = userService.getUsersByStatement(statement);

              if (page.results != null && page.results.Length > 0) {
            int i = page.startIndex;
            foreach (User usr in page.results) {
              Console.WriteLine("{0}) User with ID = '{1}', email = '{2}', and role = '{3}'" +
                  " was found.", i, usr.id, usr.email, usr.roleName);
              i++;
            }
              }
              offset += 500;
            } while (offset < page.totalResultSetSize);

            Console.WriteLine("Number of results found: {0}", page.totalResultSetSize);
              } catch (Exception ex) {
            Console.WriteLine("Failed to get all users. Exception says \"{0}\"",
            ex.Message);
              }
        }
    /// <summary>
    /// Handles the Click event of the btnGetUsers control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing
    /// the event data.</param>
    protected void OnGetUsersButtonClick(object sender, EventArgs e) {
      ConfigureUserForOAuth();

      try {
        // Get the UserService.
        UserService userService = (UserService)user.GetService(DfpService.v201208.UserService);

        // Sets defaults for page and Statement.
        UserPage page = new UserPage();
        Statement statement = new Statement();
        int offset = 0;

        DataTable dataTable = new DataTable();
        dataTable.Columns.AddRange(new DataColumn[] {
            new DataColumn("Serial No.", typeof(int)),
            new DataColumn("User Id", typeof(long)),
            new DataColumn("Email", typeof(string)),
            new DataColumn("Role", typeof(string))
        });
        do {
          // Create a Statement to get all users.
          statement.query = string.Format("LIMIT 500 OFFSET {0}", offset);

          // Get users by Statement.
          page = userService.getUsersByStatement(statement);

          if (page.results != null && page.results.Length > 0) {
            int i = page.startIndex;
            foreach (User usr in page.results) {
              DataRow dataRow = dataTable.NewRow();
              dataRow.ItemArray = new object[] {i + 1, usr.id, usr.email, usr.roleName};
              dataTable.Rows.Add(dataRow);
              i++;
            }
          }
          offset += 500;
        } while (offset < page.totalResultSetSize);
        if (dataTable.Rows.Count > 0) {
          UserGrid.DataSource = dataTable;
          UserGrid.DataBind();
        } else {
          Response.Write("No users were found.");
        }
      } catch (Exception ex) {
        Response.Write(string.Format("Failed to get users. Exception says \"{0}\"",
            ex.Message));
      }
    }
    /// <summary>
    /// The main method.
    /// </summary>
    /// <param name="args">Command line arguments.</param>
    static void Main(string[] args) {
      DfpUser user = new DfpUser();
      DfpAppConfig config = (user.Config as DfpAppConfig);
      if (config.AuthorizationMethod == DfpAuthorizationMethod.OAuth2) {
        if (config.OAuth2Mode == OAuth2Flow.APPLICATION &&
            string.IsNullOrEmpty(config.OAuth2RefreshToken)) {
          DoAuth2Authorization(user);
        }
      } else {
        throw new Exception("Authorization mode is not OAuth.");
      }

      // Get the UserService.
      UserService userService = (UserService)user.GetService(DfpService.v201208.UserService);

      // Sets defaults for page and Statement.
      UserPage page = new UserPage();
      Statement statement = new Statement();
      int offset = 0;

      try {
        do {
          // Create a Statement to get all users.
          statement.query = string.Format("LIMIT 500 OFFSET {0}", offset);

          // Get users by Statement.
          page = userService.getUsersByStatement(statement);

          if (page.results != null && page.results.Length > 0) {
            int i = page.startIndex;
            foreach (User usr in page.results) {
              Console.WriteLine("{0}) User with ID = '{1}', email = '{2}', and role = '{3}'" +
                  " was found.", i, usr.id, usr.email, usr.roleName);
              i++;
            }
          }
          offset += 500;
        } while (offset < page.totalResultSetSize);

        Console.WriteLine("Number of results found: {0}", page.totalResultSetSize);
      } catch (Exception ex) {
        Console.WriteLine("Failed to get all users. Exception says \"{0}\"",
            ex.Message);
      }
    }
    /// <summary>
    /// Run the code example.
    /// </summary>
    /// <param name="user">The DFP user object running the code example.</param>
    public override void Run(DfpUser user) {
      // Get the UserService.
      UserService userService = (UserService) user.GetService(DfpService.v201208.UserService);

      // Set the ID of the user to deactivate
      long userId = long.Parse(_T("INSERT_USER_ID_HERE"));

      // Create Statement text to select user by id.
      string statementText = "WHERE id = :userId LIMIT 500";
      Statement statement = new StatementBuilder("").AddValue("userId", userId).ToStatement();

      // Sets defaults for page and offset.
      UserPage page = new UserPage();
      int offset = 0;
      List<string> userIds = new List<string>();

      try {
        do {
          // Create a Statement to page through users.
          statement.query = string.Format("{0} OFFSET {1}", statementText, offset);

          // Get users by Statement.
          page = userService.getUsersByStatement(statement);

          if (page.results != null && page.results.Length > 0) {
            int i = page.startIndex;
            foreach (User userResult in page.results) {
              Console.WriteLine("{0}) User with ID = '{1}', email = '{2}', and status = '{3}'" +
                 " will be deactivated.", i, userResult.id, userResult.email,
                 userResult.isActive ? "ACTIVE" : "INACTIVE");
              userIds.Add(userResult.id.ToString());
              i++;
            }
          }

          offset += 500;
        } while (offset < page.totalResultSetSize);

        Console.WriteLine("Number of users to be deactivated: {0}", page.totalResultSetSize);

        if (userIds.Count > 0) {
          // Create action Statement.
          statement = new StatementBuilder(
              string.Format("WHERE id IN ({0})", string.Join(",", userIds.ToArray()))).
              ToStatement();

          // Create action.
          DeactivateUsers action = new DeactivateUsers();

          // Perform action.
          UpdateResult result = userService.performUserAction(action, statement);

          // Display results.
          if (result != null && result.numChanges > 0) {
            Console.WriteLine("Number of users deactivated: {0}" + result.numChanges);
          } else {
            Console.WriteLine("No users were deactivated.");
          }
        }
      } catch (Exception ex) {
        Console.WriteLine("Failed to deactivate users. Exception says \"{0}\"",
            ex.Message);
      }
    }