Ejemplo n.º 1
0
    /// <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.v201408.UserService);

      // Create a Statement to get all users.
      StatementBuilder statementBuilder = new StatementBuilder()
          .OrderBy("id ASC")
          .Limit(StatementBuilder.SUGGESTED_PAGE_LIMIT);

      // Sets defaults for page and Statement.
      UserPage page = new UserPage();

      try {
        do {
          // Get users by Statement.
          page = userService.getUsersByStatement(statementBuilder.ToStatement());

          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++;
            }
          }
          statementBuilder.IncreaseOffsetBy(StatementBuilder.SUGGESTED_PAGE_LIMIT);
        } while (statementBuilder.GetOffset() < 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.v201408.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.
      StatementBuilder statementBuilder = new StatementBuilder()
          .Where("id = :userId")
          .OrderBy("id ASC")
          .Limit(1)
          .AddValue("userId", userId);

      // Sets default for page.
      UserPage page = new UserPage();
      List<string> userIds = new List<string>();

      try {
        do {
          // Get users by Statement.
          page = userService.getUsersByStatement(statementBuilder.ToStatement());

          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++;
            }
          }

          statementBuilder.IncreaseOffsetBy(StatementBuilder.SUGGESTED_PAGE_LIMIT);
        } while (statementBuilder.GetOffset() < page.totalResultSetSize);

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

        if (userIds.Count > 0) {
          // Modify statement for action.
          statementBuilder.RemoveLimitAndOffset();

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

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

          // 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);
      }
    }