public void Init() {
   TestUtils utils = new TestUtils();
   placementService = (PlacementService) user.GetService(DfpService.v201403.PlacementService);
   adUnit1 = utils.CreateAdUnit(user);
   adUnit2 = utils.CreateAdUnit(user);
   placement = utils.CreatePlacement(user, new string[] {adUnit1.id, adUnit2.id});
 }
    /// <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.v201403.InventoryService);

      // Get the NetworkService.
      NetworkService networkService =
          (NetworkService) user.GetService(DfpService.v201403.NetworkService);

      string effectiveRootAdUnitId = networkService.getCurrentNetwork().effectiveRootAdUnitId;

      // Create an array to store local ad unit objects.
      AdUnit[] adUnits = new AdUnit[5];

      for (int i = 0; i < 5; i++) {
        AdUnit adUnit = new AdUnit();
        adUnit.name = string.Format("Ad_Unit_{0}", i);
        adUnit.parentId = effectiveRootAdUnitId;

        adUnit.description = "Ad unit description.";
        adUnit.targetWindow = AdUnitTargetWindow.BLANK;

        // Set the size of possible creatives that can match this ad unit.
        Size size = new Size();
        size.width = 300;
        size.height = 250;

        // Create ad unit size.
        AdUnitSize adUnitSize = new AdUnitSize();
        adUnitSize.size = size;
        adUnitSize.environmentType = EnvironmentType.BROWSER;

        adUnit.adUnitSizes = new AdUnitSize[] {adUnitSize};
        adUnits[i] = adUnit;
      }

      try {
        // Create the ad units on the server.
        adUnits = inventoryService.createAdUnits(adUnits);

        if (adUnits != null) {
          foreach (AdUnit adUnit in adUnits) {
            Console.WriteLine("An ad unit with ID = '{0}' was created under parent with " +
                "ID = '{1}'.", adUnit.id, adUnit.parentId);
          }
        } else {
          Console.WriteLine("No ad units created.");
        }
      } catch (Exception ex) {
        Console.WriteLine("Failed to create ad units. Exception says \"{0}\"", ex.Message);
      }
    }
    public void TestCreateAdUnits() {
      // Create ad unit 1.
      AdUnit localAdUnit1 = new AdUnit();
      localAdUnit1.name = string.Format("Ad_Unit_{0}", new TestUtils().GetTimeStamp());
      localAdUnit1.parentId = adUnit1.id;

      Size size1 = new Size();
      size1.width = 300;
      size1.height = 250;

      AdUnitSize adUnitSize1 = new AdUnitSize();
      adUnitSize1.size = size1;
      adUnitSize1.environmentType = EnvironmentType.BROWSER;

      localAdUnit1.adUnitSizes = new AdUnitSize[] {adUnitSize1};

      // Create ad unit 2.
      AdUnit localAdUnit2 = new AdUnit();
      localAdUnit2.name = string.Format("Ad_Unit_{0}", new TestUtils().GetTimeStamp());
      localAdUnit2.parentId = adUnit1.id;

      Size size2 = new Size();
      size2.width = 300;
      size2.height = 250;

      AdUnitSize adUnitSize2 = new AdUnitSize();
      adUnitSize2.size = size2;
      adUnitSize2.environmentType = EnvironmentType.BROWSER;

      localAdUnit2.adUnitSizes = new AdUnitSize[] {adUnitSize2};

      AdUnit[] newAdUnits = null;

      Assert.DoesNotThrow(delegate() {
        newAdUnits = inventoryService.createAdUnits(new AdUnit[] {localAdUnit1, localAdUnit2});
      });

      Assert.NotNull(newAdUnits);
      Assert.AreEqual(newAdUnits.Length, 2);

      Assert.AreEqual(newAdUnits[0].name, localAdUnit1.name);
      Assert.AreEqual(newAdUnits[0].parentId, localAdUnit1.parentId);
      Assert.AreEqual(newAdUnits[0].parentId, adUnit1.id);
      Assert.AreEqual(newAdUnits[0].status, localAdUnit1.status);
      Assert.AreEqual(newAdUnits[0].targetWindow, localAdUnit1.targetWindow);

      Assert.AreEqual(newAdUnits[1].name, localAdUnit2.name);
      Assert.AreEqual(newAdUnits[1].parentId, localAdUnit2.parentId);
      Assert.AreEqual(newAdUnits[1].parentId, adUnit1.id);
      Assert.AreEqual(newAdUnits[1].status, localAdUnit2.status);
      Assert.AreEqual(newAdUnits[1].targetWindow, localAdUnit2.targetWindow);
    }
    /// <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.v201403.InventoryService);

      // Get the NetworkService.
      NetworkService networkService =
          (NetworkService) user.GetService(DfpService.v201403.NetworkService);

      // Set the parent ad unit's ID for all ad units to be created under.
      String effectiveRootAdUnitId = networkService.getCurrentNetwork().effectiveRootAdUnitId;

      // Create local ad unit object.
      AdUnit adUnit = new AdUnit();
      adUnit.name = "Mobile_Ad_Unit";
      adUnit.parentId = effectiveRootAdUnitId;
      adUnit.description = "Ad unit description.";
      adUnit.targetWindow = AdUnitTargetWindow.BLANK;
      adUnit.targetPlatform = TargetPlatform.MOBILE;

      // Create ad unit size.
      AdUnitSize adUnitSize = new AdUnitSize();
      Size size = new Size();
      size.width = 400;
      size.height = 300;
      size.isAspectRatio = false;
      adUnitSize.size = size;
      adUnitSize.environmentType = EnvironmentType.BROWSER;

      // Set the size of possible creatives that can match this ad unit.
      adUnit.adUnitSizes = new AdUnitSize[] {adUnitSize};

      try {
        // Create the ad unit on the server.
        AdUnit[] createdAdUnits = inventoryService.createAdUnits(new AdUnit[] {adUnit});

        foreach (AdUnit createdAdunit in createdAdUnits) {
          Console.WriteLine("An ad unit with ID \"{0}\" was created under parent with ID " +
              "\"{1}\".", createdAdunit.id, createdAdunit.parentId);
        }
      } catch (Exception ex) {
        Console.WriteLine("Failed to create ad units. 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 InventoryService.
      InventoryService inventoryService =
          (InventoryService) user.GetService(DfpService.v201403.InventoryService);

      // Get the NetworkService.
      NetworkService networkService =
          (NetworkService) user.GetService(DfpService.v201403.NetworkService);

      // Set the parent ad unit's ID for all ad units to be created under.
      String effectiveRootAdUnitId = networkService.getCurrentNetwork().effectiveRootAdUnitId;

      // Create local ad unit object.
      AdUnit adUnit = new AdUnit();
      adUnit.name = "Video_Ad_Unit";
      adUnit.parentId = effectiveRootAdUnitId;
      adUnit.description = "Ad unit description.";
      adUnit.targetWindow = AdUnitTargetWindow.BLANK;
      adUnit.explicitlyTargeted = true;
      adUnit.targetPlatform = TargetPlatform.WEB;

      // Create master ad unit size.
      AdUnitSize masterAdUnitSize = new AdUnitSize();
      Size size1 = new Size();
      size1.width = 400;
      size1.height = 300;
      size1.isAspectRatio = false;
      masterAdUnitSize.size = size1;
      masterAdUnitSize.environmentType = EnvironmentType.VIDEO_PLAYER;

      // Create companion sizes.
      AdUnitSize companionAdUnitSize1 = new AdUnitSize();
      Size size2 = new Size();
      size2.width = 300;
      size2.height = 250;
      size2.isAspectRatio = false;
      companionAdUnitSize1.size = size2;
      companionAdUnitSize1.environmentType = EnvironmentType.BROWSER;

      AdUnitSize companionAdUnitSize2 = new AdUnitSize();
      Size size3 = new Size();
      size3.width = 728;
      size3.height = 90;
      size3.isAspectRatio = false;
      companionAdUnitSize2.size = size3;
      companionAdUnitSize2.environmentType = EnvironmentType.BROWSER;

      // Add companions to master ad unit size.
      masterAdUnitSize.companions = new AdUnitSize[] {companionAdUnitSize1, companionAdUnitSize2};

      // Set the size of possible creatives that can match this ad unit.
      adUnit.adUnitSizes = new AdUnitSize[] {masterAdUnitSize};

      try {
        // Create the ad unit on the server.
        AdUnit[] createdAdUnits = inventoryService.createAdUnits(new AdUnit[] {adUnit});

        foreach (AdUnit createdAdUnit in createdAdUnits) {
          Console.WriteLine("A video ad unit with ID \"{0}\" was created under parent with ID " +
              "\"{1}\".", createdAdUnit.id, createdAdUnit.parentId);
        }
      } catch (Exception ex) {
        Console.WriteLine("Failed to create video ad units. Exception says \"{0}\"", ex.Message);
      }
    }
        /// <summary>
        /// Builds and displays an ad unit tree from an array of ad units underneath
        /// the root ad unit.
        /// </summary>
        /// <param name="root">The root ad unit to build the tree under.</param>
        /// <param name="units">The array of ad units.</param>
        private static void BuildAndDisplayAdUnitTree(AdUnit root, AdUnit[] units)
        {
            Dictionary<String, List<AdUnit>> treeMap = new Dictionary<String, List<AdUnit>>();

              foreach (AdUnit unit in units) {
            if (unit.parentId != null) {
              if (treeMap.ContainsKey(unit.parentId) == false) {
            treeMap.Add(unit.parentId, new List<AdUnit>());
              }
              treeMap[unit.parentId].Add(unit);
            }
              }

              if (root != null) {
            DisplayInventoryTree(root, treeMap);
              } else {
            Console.WriteLine("No root unit found.");
              }
        }
        /// <summary>
        /// Helper for displaying inventory units.
        /// </summary>
        /// <param name="root">The root inventory unit.</param>
        /// <param name="treeMap">The map of id to List of inventory units.</param>
        /// <param name="depth">The depth the tree has reached.</param>
        private static void DisplayInventoryTreeHelper(AdUnit root,
        Dictionary<String, List<AdUnit>> treeMap, int depth)
        {
            Console.WriteLine(GenerateTab(depth) + root.name + " (" + root.id + ")");

              if (treeMap.ContainsKey(root.id)) {
            foreach (AdUnit child in treeMap[root.id]) {
              DisplayInventoryTreeHelper(child, treeMap, depth + 1);
            }
              }
        }
 /// <summary>
 /// Displays the ad unit tree beginning at the root ad unit.
 /// </summary>
 /// <param name="root">The root ad unit</param>
 /// <param name="treeMap">The map of id to list of ad units</param>
 private static void DisplayInventoryTree(AdUnit root, Dictionary<String,
 List<AdUnit>> treeMap)
 {
     DisplayInventoryTreeHelper(root, treeMap, 0);
 }
 public void Init() {
   TestUtils utils = new TestUtils();
   inventoryService = (InventoryService) user.GetService(DfpService.v201403.InventoryService);
   adUnit1 = utils.CreateAdUnit(user);
   adUnit2 = utils.CreateAdUnit(user);
 }
    public AdUnit CreateAdUnit(DfpUser user) {
      InventoryService inventoryService =
          (InventoryService) user.GetService(DfpService.v201403.InventoryService);

      AdUnit adUnit = new AdUnit();
      adUnit.name = string.Format("Ad_Unit_{0}", GetTimeStamp());
      adUnit.parentId = FindRootAdUnit(user).id;

      // Set the size of possible creatives that can match this ad unit.
      Size size = new Size();
      size.width = 300;
      size.height = 250;

      // Create ad unit size.
      AdUnitSize adUnitSize = new AdUnitSize();
      adUnitSize.size = size;
      adUnitSize.environmentType = EnvironmentType.BROWSER;

      adUnit.adUnitSizes = new AdUnitSize[] {adUnitSize};
      return inventoryService.createAdUnits(new AdUnit[] {adUnit})[0];
    }