/// <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 InventoryService. InventoryService inventoryService = (InventoryService) user.GetService(DfpService.v201302.InventoryService); // Sets defaults for page and statement. AdUnitPage page = new AdUnitPage(); Statement statement = new Statement(); int offset = 0; try { do { // Create a Statement to get all ad units. statement.query = string.Format("LIMIT 500 OFFSET {0}", offset); // Get ad units by Statement. page = inventoryService.getAdUnitsByStatement(statement); if (page.results != null && page.results.Length > 0) { int i = page.startIndex; foreach (AdUnit adUnit in page.results) { Console.WriteLine("{0}) Ad unit with ID = '{1}', name = '{2}' and status = '{3}' " + "was found.", i, adUnit.id, adUnit.name, adUnit.status); i++; } } offset += 500; } while (offset < page.totalResultSetSize); Console.WriteLine("Number of results found: {0}", page.totalResultSetSize); } catch (Exception ex) { Console.WriteLine("Failed to get ad unit. Exception says \"{0}\"", ex.Message); } }
/// <summary> /// Gets all ad units for this user. /// </summary> /// <param name="user">The DfpUser to get the ad units for.</param> /// <returns>All ad units for this user.</returns> private static AdUnit[] GetAllAdUnits(DfpUser user) { // Create list to hold all ad units. List<AdUnit> adUnits = new List<AdUnit>(); // Get InventoryService. InventoryService inventoryService = (InventoryService) user.GetService(DfpService.v201302.InventoryService); // Sets defaults for page and Statement. AdUnitPage page = new AdUnitPage(); Statement statement = new Statement(); int offset = 0; do { // Create a Statement to get all ad units. statement.query = string.Format("LIMIT 500 OFFSET {0}", offset); // Get ad units by Statement. page = inventoryService.getAdUnitsByStatement(statement); if (page.results != null && page.results.Length > 0) { adUnits.AddRange(page.results); } offset += 500; } while (page.results != null && page.results.Length == 500); return adUnits.ToArray(); }
/// <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 InventoryService. InventoryService inventoryService = (InventoryService) user.GetService(DfpService.v201302.InventoryService); // Create Statement text to select active ad units. string statementText = "WHERE status = :status LIMIT 500"; Statement statement = new StatementBuilder("").AddValue("status", InventoryStatus.ACTIVE.ToString()).ToStatement(); // Sets defaults for page and offset. AdUnitPage page = new AdUnitPage(); int offset = 0; List<string> adUnitIds = new List<string>(); try { do { // Create a Statement to page through active ad units. statement.query = string.Format("{0} OFFSET {1}", statementText, offset); // Get ad units by Statement. page = inventoryService.getAdUnitsByStatement(statement); if (page.results != null && page.results.Length > 0) { int i = page.startIndex; foreach (AdUnit adUnit in page.results) { Console.WriteLine("{0}) Ad unit with ID ='{1}', name = {2} and status = {3} will" + " be deactivated.", i, adUnit.id, adUnit.name, adUnit.status); adUnitIds.Add(adUnit.id); i++; } } offset += 500; } while (offset < page.totalResultSetSize); Console.WriteLine("Number of ad units to be deactivated: {0}", adUnitIds.Count); if (adUnitIds.Count > 0) { // Create action Statement. statement = new StatementBuilder( string.Format("WHERE id IN ({0})", string.Join(",", adUnitIds.ToArray()))). ToStatement(); // Create action. DeactivateAdUnits action = new DeactivateAdUnits(); // Perform action. UpdateResult result = inventoryService.performAdUnitAction(action, statement); // Display results. if (result != null && result.numChanges > 0) { Console.WriteLine("Number of ad units deactivated: {0}", result.numChanges); } else { Console.WriteLine("No ad units were deactivated."); } } } catch (Exception ex) { Console.WriteLine("Failed to deactivate ad units. Exception says \"{0}\"", ex.Message); } }