Google.Api.Ads.AdWords.Examples.CSharp.v201306.ValidateTextAd.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 text ads are /// added.
return void
        public void Run(AdWordsUser user, long adGroupId)
        {
            // Get the AdGroupAdService.
              AdGroupAdService adGroupAdService =
              (AdGroupAdService) user.GetService(AdWordsService.v201306.AdGroupAdService);

              // Set the validateOnly headers.
              adGroupAdService.RequestHeader.validateOnly = true;

              // Create your 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";

              AdGroupAd textAdGroupAd = new AdGroupAd();
              textAdGroupAd.adGroupId = adGroupId;
              textAdGroupAd.ad = textAd;

              AdGroupAdOperation textAdOperation = new AdGroupAdOperation();
              textAdOperation.@operator = Operator.ADD;
              textAdOperation.operand = textAdGroupAd;

              try {
            AdGroupAdReturnValue retVal = adGroupAdService.mutate(
            (new AdGroupAdOperation[] {textAdOperation}));
            // Since validation is ON, result will be null.
            Console.WriteLine("text ad validated successfully.");
              } catch (AdWordsApiException ex) {
            // This block will be hit if there is a validation error from the server.
            Console.WriteLine("There were validation error(s) while adding text ad.");

            if (ex.ApiException != null) {
              foreach (ApiError error in ((ApiException) ex.ApiException).errors) {
            Console.WriteLine("  Error type is '{0}' and fieldPath is '{1}'.",
                error.ApiErrorType, error.fieldPath);
              }
            }
              } catch (Exception ex) {
            throw new System.ApplicationException("Failed to validate 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)
 {
     ValidateTextAd codeExample = new ValidateTextAd();
       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));
       }
 }
ValidateTextAd