public void Run(AdWordsUser user, long adGroupId)
{
// Get the AdGroupCriterionService.
AdGroupCriterionService adGroupCriterionService =
(AdGroupCriterionService) user.GetService(
AdWordsService.v201306.AdGroupCriterionService);
List<AdGroupCriterionOperation> operations = new List<AdGroupCriterionOperation>();
for (int i = 0; i < NUM_ITEMS; i++) {
// Create the keyword.
Keyword keyword = new Keyword();
keyword.text = "mars cruise";
keyword.matchType = KeywordMatchType.BROAD;
// Create the biddable ad group criterion.
BiddableAdGroupCriterion keywordCriterion = new BiddableAdGroupCriterion();
keywordCriterion.adGroupId = adGroupId;
keywordCriterion.criterion = keyword;
// Optional: Set the user status.
keywordCriterion.userStatus = UserStatus.PAUSED;
// Optional: Set the keyword destination url.
keywordCriterion.destinationUrl = "http://example.com/mars/cruise/" + i;
// Create the operations.
AdGroupCriterionOperation operation = new AdGroupCriterionOperation();
operation.@operator = Operator.ADD;
operation.operand = keywordCriterion;
operations.Add(operation);
}
try {
// Create the keywords.
AdGroupCriterionReturnValue retVal = adGroupCriterionService.mutate(operations.ToArray());
// Display the results.
if (retVal != null && retVal.value != null) {
foreach (AdGroupCriterion adGroupCriterion in retVal.value) {
// If you are adding multiple type of criteria, then you may need to
// check for
//
// if (adGroupCriterion is Keyword) { ... }
//
// to identify the criterion type.
Console.WriteLine("Keyword with ad group id = '{0}', keyword id = '{1}', text = " +
"'{2}' and match type = '{3}' was created.", adGroupCriterion.adGroupId,
adGroupCriterion.criterion.id, (adGroupCriterion.criterion as Keyword).text,
(adGroupCriterion.criterion as Keyword).matchType);
}
} else {
Console.WriteLine("No keywords were added.");
}
} catch (Exception ex) {
throw new System.ApplicationException("Failed to create keywords.", ex);
}
}