Google.Api.Ads.AdWords.Examples.CSharp.v201306.UpdatePlacement.Run C# (CSharp) Method

Run() public method

Runs the code example.
public Run ( Google.Api.Ads.AdWords.Lib.AdWordsUser user, long adGroupId, long placementId ) : void
user Google.Api.Ads.AdWords.Lib.AdWordsUser The AdWords user.
adGroupId long Id of the ad group that contains the placement. ///
placementId long Id of the placement to be updated.
return void
        public void Run(AdWordsUser user, long adGroupId, long placementId)
        {
            // Get the AdGroupCriterionService.
              AdGroupCriterionService adGroupCriterionService =
              (AdGroupCriterionService) user.GetService(AdWordsService.v201306.AdGroupCriterionService);

              // Since we are not updating any placement-specific fields, it is enough to
              // create a criterion object.
              Criterion criterion = new Criterion();
              criterion.id = placementId;

              // Create ad group criterion.
              BiddableAdGroupCriterion biddableAdGroupCriterion = new BiddableAdGroupCriterion();
              biddableAdGroupCriterion.adGroupId = adGroupId;
              biddableAdGroupCriterion.criterion = criterion;

              // Create the bids.
              BiddingStrategyConfiguration biddingConfig = new BiddingStrategyConfiguration();
              CpmBid cpmBid = new CpmBid();
              cpmBid.bid = new Money();
              cpmBid.bid.microAmount = 1000000;
              biddingConfig.bids = new Bids[] {cpmBid};

              biddableAdGroupCriterion.biddingStrategyConfiguration = biddingConfig;

              // Create the operation.
              AdGroupCriterionOperation operation = new AdGroupCriterionOperation();
              operation.@operator = Operator.SET;
              operation.operand = biddableAdGroupCriterion;

              try {
            // Update the placement.
            AdGroupCriterionReturnValue retVal =
            adGroupCriterionService.mutate(new AdGroupCriterionOperation[] {operation});

            // Display the results.
            if (retVal != null && retVal.value != null && retVal.value.Length > 0) {
              AdGroupCriterion adGroupCriterion = retVal.value[0];
              long bidAmount = 0;
              foreach (Bids bids in (adGroupCriterion as BiddableAdGroupCriterion).
              biddingStrategyConfiguration.bids) {
            if (bids is CpmBid) {
              bidAmount = (bids as CpmBid).bid.microAmount;
              break;
            }
              }
              Console.WriteLine("Placement with ad group id = '{0}', id = '{1}' was updated with " +
              "bid amount = '{2}' micros.", adGroupCriterion.adGroupId,
              adGroupCriterion.criterion.id, bidAmount);
            } else {
              Console.WriteLine("No placement was updated.");
            }
              } catch (Exception ex) {
            throw new System.ApplicationException("Failed to update placement.", ex);
              }
        }

Usage Example

 /// <summary>
 /// Main method, to run this code example as a standalone application.
 /// </summary>
 /// <param name="args">The command line arguments.</param>
 public static void Main(string[] args)
 {
     UpdatePlacement codeExample = new UpdatePlacement();
       Console.WriteLine(codeExample.Description);
       try {
     long adGroupId = long.Parse("INSERT_ADGROUP_ID_HERE");
     long placementId = long.Parse("INSERT_PLACEMENT_ID_HERE");
     codeExample.Run(new AdWordsUser(), adGroupId, placementId);
       } catch (Exception ex) {
     Console.WriteLine("An exception occurred while running this code example. {0}",
     ExampleUtilities.FormatException(ex));
       }
 }
UpdatePlacement