public void Run(AdWordsUser user, long campaignId)
{
// Get the AdGroupService.
AdGroupService adGroupService =
(AdGroupService) user.GetService(AdWordsService.v201306.AdGroupService);
// Create the selector.
Selector selector = new Selector();
selector.fields = new string[] {"Id", "Name"};
// Create the filters.
Predicate predicate = new Predicate();
predicate.field = "CampaignId";
predicate.@operator = PredicateOperator.EQUALS;
predicate.values = new string[] {campaignId.ToString()};
selector.predicates = new Predicate[] {predicate};
// Set the selector paging.
selector.paging = new Paging();
int offset = 0;
int pageSize = 500;
AdGroupPage page = new AdGroupPage();
try {
do {
selector.paging.startIndex = offset;
selector.paging.numberResults = pageSize;
// Get the ad groups.
page = adGroupService.get(selector);
// Display the results.
if (page != null && page.entries != null) {
int i = offset;
foreach (AdGroup adGroup in page.entries) {
Console.WriteLine("{0}) Ad group name is '{1}' and id is {2}.", i + 1, adGroup.name,
adGroup.id);
i++;
}
}
offset += pageSize;
} while (offset < page.totalNumEntries);
Console.WriteLine("Number of ad groups found: {0}", page.totalNumEntries);
} catch (Exception ex) {
throw new System.ApplicationException("Failed to retrieve ad groups.", ex);
}
}