public void Run(AdWordsUser user, long adGroupId)
{
// Get the AdGroupAdService.
AdGroupAdService service =
(AdGroupAdService) user.GetService(AdWordsService.v201306.AdGroupAdService);
List<AdGroupAdOperation> operations = new List<AdGroupAdOperation>();
for (int i = 0; i < NUM_ITEMS; i++) {
// Create the text ad.
TextAd textAd = new TextAd();
textAd.headline = "Luxury Cruise to Mars";
textAd.description1 = "Visit the Red Planet in style.";
textAd.description2 = "Low-gravity fun for everyone!";
textAd.displayUrl = "www.example.com";
textAd.url = "http://www.example.com/" + i;
AdGroupAd textAdGroupAd = new AdGroupAd();
textAdGroupAd.adGroupId = adGroupId;
textAdGroupAd.ad = textAd;
// Optional: Set the status.
textAdGroupAd.status = AdGroupAdStatus.PAUSED;
// Create the operation.
AdGroupAdOperation operation = new AdGroupAdOperation();
operation.@operator = Operator.ADD;
operation.operand = textAdGroupAd;
operations.Add(operation);
}
AdGroupAdReturnValue retVal = null;
try {
// Create the ads.
retVal = service.mutate(operations.ToArray());
// Display the results.
if (retVal != null && retVal.value != null) {
// If you are adding multiple type of Ads, then you may need to check
// for
//
// if (adGroupAd.ad is TextAd) { ... }
//
// to identify the ad type.
foreach (AdGroupAd adGroupAd in retVal.value) {
Console.WriteLine("New text ad with id = \"{0}\" and displayUrl = \"{1}\" was created.",
adGroupAd.ad.id, adGroupAd.ad.displayUrl);
}
} else {
Console.WriteLine("No text ads were created.");
}
} catch (Exception ex) {
throw new System.ApplicationException("Failed to create text ad.", ex);
}
}