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

Run() public method

Runs the code example.
public Run ( Google.Api.Ads.AdWords.Lib.AdWordsUser user, long adGroupId ) : void
user Google.Api.Ads.AdWords.Lib.AdWordsUser The AdWords user.
adGroupId long Id of the ad group to which ads are added. ///
return void
        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);
              }
        }

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