Beispiel #1
0
        /// <summary>
        /// Run the code example.
        /// </summary>
        /// <param name="dfpUser">The DFP user object running the code example.</param>
        public override void Run(DfpUser dfpUser)
        {
            // Get the UserTeamAssociationService.
            UserTeamAssociationService userTeamAssociationService = (UserTeamAssociationService)
                                                                    dfpUser.GetService(DfpService.v201502.UserTeamAssociationService);

            // Set the user to remove from its teams.
            long userId = long.Parse(_T("INSERT_USER_ID_HERE"));

            // Create filter text to select user team associations by the user ID.
            StatementBuilder statementBuilder = new StatementBuilder()
                                                .Where("userId = :userId")
                                                .OrderBy("userId ASC, teamId ASC")
                                                .Limit(StatementBuilder.SUGGESTED_PAGE_LIMIT)
                                                .AddValue("userId", userId);

            // Set default for page.
            UserTeamAssociationPage page = new UserTeamAssociationPage();

            try {
                do
                {
                    // Get user team associations by statement.
                    page = userTeamAssociationService.getUserTeamAssociationsByStatement(
                        statementBuilder.ToStatement());

                    if (page.results != null)
                    {
                        int i = page.startIndex;
                        foreach (UserTeamAssociation userTeamAssociation in page.results)
                        {
                            Console.WriteLine("{0}) User team association between user with ID \"{1}\" and " +
                                              "team with ID \"{2}\" will be deleted.", i, userTeamAssociation.userId,
                                              userTeamAssociation.teamId);
                            i++;
                        }
                    }

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

                Console.WriteLine("Number of teams that the user will be removed from: "
                                  + page.totalResultSetSize);

                if (page.totalResultSetSize > 0)
                {
                    // Modify statement for action.
                    statementBuilder.RemoveLimitAndOffset();

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

                    // Perform action.
                    UpdateResult result = userTeamAssociationService.performUserTeamAssociationAction(action,
                                                                                                      statementBuilder.ToStatement());

                    // Display results.
                    if (result != null && result.numChanges > 0)
                    {
                        Console.WriteLine("Number of teams that the user was removed from: "
                                          + result.numChanges);
                    }
                    else
                    {
                        Console.WriteLine("No user team associations were deleted.");
                    }
                }
            } catch (Exception ex) {
                Console.WriteLine("Failed to delete user team associations. Exception says \"{0}\"",
                                  ex.Message);
            }
        }
        /// <summary>
        /// Run the code example.
        /// </summary>
        /// <param name="dfpUser">The DFP user object running the code example.</param>
        public override void Run(DfpUser dfpUser)
        {
            // Get the UserTeamAssociationService.
            UserTeamAssociationService userTeamAssociationService = (UserTeamAssociationService)
                                                                    dfpUser.GetService(DfpService.v201311.UserTeamAssociationService);

            // Set the user to remove from its teams.
            long userId = long.Parse(_T("INSERT_USER_ID_HERE"));

            // Create filter text to select user team associations by the user ID.
            String    statementText   = "WHERE userId = :userId LIMIT 500";
            Statement filterStatement = new StatementBuilder("").AddValue("userId", userId).
                                        ToStatement();

            // Set defaults for page and offset.
            UserTeamAssociationPage page = new UserTeamAssociationPage();
            int offset = 0;

            try {
                do
                {
                    // Create a statement to page through user team associations.
                    filterStatement.query = statementText + " OFFSET " + offset;

                    // Get user team associations by statement.
                    page = userTeamAssociationService.getUserTeamAssociationsByStatement(filterStatement);

                    if (page.results != null)
                    {
                        int i = page.startIndex;
                        foreach (UserTeamAssociation userTeamAssociation in page.results)
                        {
                            Console.WriteLine("{0}) User team association between user with ID \"{1}\" and " +
                                              "team with ID \"{2}\" will be deleted.", i, userTeamAssociation.userId,
                                              userTeamAssociation.teamId);
                            i++;
                        }
                    }

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

                Console.WriteLine("Number of teams that the user will be removed from: "
                                  + page.totalResultSetSize);

                if (page.totalResultSetSize > 0)
                {
                    // Modify statement for action.
                    filterStatement.query = "WHERE userId = :userId";

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

                    // Perform action.
                    UpdateResult result =
                        userTeamAssociationService.performUserTeamAssociationAction(action, filterStatement);

                    // Display results.
                    if (result != null && result.numChanges > 0)
                    {
                        Console.WriteLine("Number of teams that the user was removed from: "
                                          + result.numChanges);
                    }
                    else
                    {
                        Console.WriteLine("No user team associations were deleted.");
                    }
                }
            } catch (Exception ex) {
                Console.WriteLine("Failed to delete user team associations. Exception says \"{0}\"",
                                  ex.Message);
            }
        }