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

Run() public method

Runs the code example.
public Run ( Google.Api.Ads.AdWords.Lib.AdWordsUser user, long campaignId ) : void
user Google.Api.Ads.AdWords.Lib.AdWordsUser The AdWords user.
campaignId long Id of the campaign for which ad groups are /// retrieved.
return void
        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);
              }
        }

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)
 {
     GetAdGroups codeExample = new GetAdGroups();
       Console.WriteLine(codeExample.Description);
       try {
     long campaignId = long.Parse("INSERT_CAMPAIGN_ID_HERE");
     codeExample.Run(new AdWordsUser(), campaignId);
       } catch (Exception ex) {
     Console.WriteLine("An exception occurred while running this code example. {0}",
     ExampleUtilities.FormatException(ex));
       }
 }
GetAdGroups