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

Run() public method

Runs the code example.
public Run ( Google.Api.Ads.AdWords.Lib.AdWordsUser user ) : void
user Google.Api.Ads.AdWords.Lib.AdWordsUser The AdWords user.
return void
        public void Run(AdWordsUser user)
        {
            // Get the AdGroupCriterionService.
              AdGroupCriterionService adGroupCriterionService =
              (AdGroupCriterionService) user.GetService(AdWordsService.v201306.AdGroupCriterionService);

              // Create a selector.
              Selector selector = new Selector();
              selector.fields = new string[] {"Id", "AdGroupId", "KeywordText"};

              // Select only keywords.
              Predicate predicate = new Predicate();
              predicate.field = "CriteriaType";
              predicate.@operator = PredicateOperator.EQUALS;
              predicate.values = new string[] {"KEYWORD"};
              selector.predicates = new Predicate[] {predicate};

              // Set the selector paging.
              selector.paging = new Paging();

              int offset = 0;
              int pageSize = 500;

              AdGroupCriterionPage page = new AdGroupCriterionPage();

              try {
            do {
              selector.paging.startIndex = offset;
              selector.paging.numberResults = pageSize;

              // Get the keywords.
              page = adGroupCriterionService.get(selector);

              // Display the results.
              if (page != null && page.entries != null) {
            int i = offset;

            foreach (AdGroupCriterion adGroupCriterion in page.entries) {
              bool isNegative = (adGroupCriterion is NegativeAdGroupCriterion);

              // If you are retrieving multiple type of criteria, then you may
              // need to check for
              //
              // if (adGroupCriterion is Keyword) { ... }
              //
              // to identify the criterion type.
              Keyword keyword = (Keyword) adGroupCriterion.criterion;
              if (isNegative) {
                Console.WriteLine("{0}) Negative keyword with ad group ID = '{1}', keyword ID " +
                    "= '{2}', and text = '{3}' was found.", i + 1, adGroupCriterion.adGroupId,
                    keyword.id, keyword.text);
              } else {
                Console.WriteLine("{0}) Keyword with ad group ID = '{1}', keyword ID = '{2}', " +
                    "text = '{3}' and matchType = '{4} was found.", i + 1,
                    adGroupCriterion.adGroupId, keyword.id, keyword.text, keyword.matchType);
              }
              i++;
            }
              }
              offset += pageSize;
            } while (offset < page.totalNumEntries);
            Console.WriteLine("Number of keywords found: {0}", page.totalNumEntries);
              } catch (Exception ex) {
            throw new System.ApplicationException("Failed to retrieve keywords.", 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)
 {
     GetKeywords codeExample = new GetKeywords();
       Console.WriteLine(codeExample.Description);
       try {
     codeExample.Run(new AdWordsUser());
       } catch (Exception ex) {
     Console.WriteLine("An exception occurred while running this code example. {0}",
     ExampleUtilities.FormatException(ex));
       }
 }
GetKeywords