/// <summary>
        /// Creates a test ThirdPartyRedirectAd for running further tests.
        /// </summary>
        /// <param name="user">The AdWords user.</param>
        /// <param name="adGroupId">The adgroup id for which the ad is created.
        /// </param>
        /// <param name="hasAdParam">True, if an ad param placeholder should be
        /// added.</param>
        /// <returns>The text ad id.</returns>
        public long CreateThirdPartyRedirectAd(AdWordsUser user, long adGroupId)
        {
            AdGroupAdService adGroupAdService =
              (AdGroupAdService) user.GetService(AdWordsService.v201309.AdGroupAdService);
              AdGroupAdOperation adGroupAdOperation = new AdGroupAdOperation();
              adGroupAdOperation.@operator = Operator.ADD;
              adGroupAdOperation.operand = new AdGroupAd();
              adGroupAdOperation.operand.adGroupId = adGroupId;

              // Create the third party redirect ad.
              ThirdPartyRedirectAd redirectAd = new ThirdPartyRedirectAd();
              redirectAd.name = string.Format("Example third party ad #{0}", this.GetTimeStamp());
              redirectAd.url = "http://www.example.com";

              redirectAd.dimensions = new Dimensions();
              redirectAd.dimensions.height = 250;
              redirectAd.dimensions.width = 300;

              // This field normally contains the javascript ad tag.
              redirectAd.snippet =
              "<img src=\"http://www.google.com/intl/en/adwords/select/images/samples/inline.jpg\"/>";
              redirectAd.impressionBeaconUrl = "http://www.examples.com/beacon";
              redirectAd.certifiedVendorFormatId = 119;
              redirectAd.isCookieTargeted = false;
              redirectAd.isUserInterestTargeted = false;
              redirectAd.isTagged = false;

              adGroupAdOperation.operand.ad = redirectAd;

              AdGroupAdReturnValue retVal =
              adGroupAdService.mutate(new AdGroupAdOperation[] {adGroupAdOperation});
              return retVal.value[0].ad.id;
        }
        /// <summary>
        /// Runs the code example.
        /// </summary>
        /// <param name="user">The AdWords user.</param>
        /// <param name="adGroupId">Id of the ad group to which ads are added.
        /// </param>
        public void Run(AdWordsUser user, long adGroupId)
        {
            // Get the AdGroupAdService.
              AdGroupAdService service =
              (AdGroupAdService) user.GetService(AdWordsService.v201309.AdGroupAdService);

              // Create standard third party redirect ad.
              ThirdPartyRedirectAd standardAd = new ThirdPartyRedirectAd();
              standardAd.name = String.Format("Example third party ad #{0}",
              ExampleUtilities.GetRandomString());
              standardAd.url = "http://www.example.com";

              standardAd.dimensions = new Dimensions();
              standardAd.dimensions.height = 250;
              standardAd.dimensions.width = 300;

              standardAd.snippet = "<img src=\"http://goo.gl/HJM3L\"/>";

              // DoubleClick Rich Media Expandable format ID.
              standardAd.certifiedVendorFormatId = 232;
              standardAd.isCookieTargeted = false;
              standardAd.isUserInterestTargeted = false;
              standardAd.isTagged = false;
              standardAd.richMediaAdType = RichMediaAdRichMediaAdType.STANDARD;

              // Expandable Ad properties.
              standardAd.expandingDirections = new ThirdPartyRedirectAdExpandingDirection[] {
              ThirdPartyRedirectAdExpandingDirection.EXPANDING_UP,
              ThirdPartyRedirectAdExpandingDirection.EXPANDING_DOWN
              };

              standardAd.adAttributes = new RichMediaAdAdAttribute[] {
              RichMediaAdAdAttribute.ROLL_OVER_TO_EXPAND};

              // Create in-stream third party redirect ad.
              ThirdPartyRedirectAd inStreamAd = new ThirdPartyRedirectAd();
              inStreamAd.name = String.Format("Example third party ad #{0}",
              ExampleUtilities.GetRandomString());
              inStreamAd.url = "http://www.example.com";
              // Set the duration to 15 secs.
              inStreamAd.adDuration = 15000;
              inStreamAd.sourceUrl = "http://ad.doubleclick.net/pfadx/N270.126913.6102203221521/B3876671.21;dcadv=2215309;sz=0x0;ord=%5btimestamp%5d;dcmt=text/xml";
              inStreamAd.certifiedVendorFormatId = 303;
              inStreamAd.richMediaAdType = RichMediaAdRichMediaAdType.IN_STREAM_VIDEO;

              List<AdGroupAdOperation> operations = new List<AdGroupAdOperation>();

              foreach (ThirdPartyRedirectAd redirectAd in new
              ThirdPartyRedirectAd[] {standardAd, inStreamAd}) {
            // Set the ad group id.
            AdGroupAd adGroupAd = new AdGroupAd();
            adGroupAd.adGroupId = adGroupId;
            adGroupAd.ad = redirectAd;

            // Create the operation.
            AdGroupAdOperation operation = new AdGroupAdOperation();
            operation.@operator = Operator.ADD;
            operation.operand = adGroupAd;

            operations.Add(operation);
              }

              AdGroupAdReturnValue retVal = null;

              try {
            // Create the ads
            retVal = service.mutate(operations.ToArray());
            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 ThirdPartyRedirectAd) { ... }
              //
              // to identify the ad type.
              foreach (AdGroupAd newAdGroupAd in retVal.value) {
            Console.WriteLine("New third party redirect ad with url = \"{0}\" and id = {1}" +
                " was created.", ((ThirdPartyRedirectAd) newAdGroupAd.ad).url,
                newAdGroupAd.ad.id);
              }
            } else {
              Console.WriteLine("No third party redirect ads were created.");
            }
              } catch (Exception ex) {
            throw new System.ApplicationException("Failed to create third party redirect ads.", ex);
              }
        }
        /// <summary>
        /// Runs the code example.
        /// </summary>
        /// <param name="user">The AdWords user.</param>
        /// <param name="adGroupId">Id of the ad group to which ads are added.
        /// </param>
        public void Run(AdWordsUser user, long adGroupId)
        {
            // Get the AdGroupAdService.
              AdGroupAdService service =
              (AdGroupAdService) user.GetService(AdWordsService.v201309.AdGroupAdService);

              // Create the third party redirect ad that violates a policy.
              ThirdPartyRedirectAd redirectAd = new ThirdPartyRedirectAd();
              redirectAd.name = "Policy violation demo ad " + ExampleUtilities.GetRandomString();
              redirectAd.url = "gopher://gopher.google.com";
              redirectAd.dimensions = new Dimensions();
              redirectAd.dimensions.width = 300;
              redirectAd.dimensions.height = 250;

              redirectAd.snippet = "<img src=\"https://sandbox.google.com/sandboximages/image.jpg\"/>";
              redirectAd.impressionBeaconUrl = "http://www.examples.com/beacon1";
              redirectAd.certifiedVendorFormatId = 119;
              redirectAd.isCookieTargeted = false;
              redirectAd.isUserInterestTargeted = false;
              redirectAd.isTagged = false;

              AdGroupAd redirectAdGroupAd = new AdGroupAd();
              redirectAdGroupAd.adGroupId = adGroupId;
              redirectAdGroupAd.ad = redirectAd;

              // Create the operations.
              AdGroupAdOperation redirectAdOperation = new AdGroupAdOperation();
              redirectAdOperation.@operator = Operator.ADD;
              redirectAdOperation.operand = redirectAdGroupAd;

              try {
            AdGroupAdReturnValue retVal = null;

            // Setup two arrays, one to hold the list of all operations to be
            // validated, and another to hold the list of operations that cannot be
            // fixed after validation.
            List<AdGroupAdOperation> allOperations = new List<AdGroupAdOperation>();
            List<AdGroupAdOperation> operationsToBeRemoved = new List<AdGroupAdOperation>();

            allOperations.Add(redirectAdOperation);

            try {
              // Validate the operations.
              service.RequestHeader.validateOnly = true;
              retVal = service.mutate(allOperations.ToArray());
            } catch (AdWordsApiException ex) {
              ApiException innerException = ex.ApiException as ApiException;
              if (innerException == null) {
            throw new Exception("Failed to retrieve ApiError. See inner exception for more " +
                "details.", ex);
              }

              // Examine each ApiError received from the server.
              foreach (ApiError apiError in innerException.errors) {
            int index = ErrorUtilities.GetOperationIndex(apiError.fieldPath);
            if (index == -1) {
              // This API error is not associated with an operand, so we cannot
              // recover from this error by removing one or more operations.
              // Rethrow the exception for manual inspection.
              throw;
            }

            // Handle policy violation errors.
            if (apiError is PolicyViolationError) {
              PolicyViolationError policyError = (PolicyViolationError) apiError;

              if (policyError.isExemptable) {
                // If the policy violation error is exemptable, add an exemption
                // request.
                List<ExemptionRequest> exemptionRequests = new List<ExemptionRequest>();
                if (allOperations[index].exemptionRequests != null) {
                  exemptionRequests.AddRange(allOperations[index].exemptionRequests);
                }

                ExemptionRequest exemptionRequest = new ExemptionRequest();
                exemptionRequest.key = policyError.key;
                exemptionRequests.Add(exemptionRequest);
                allOperations[index].exemptionRequests = exemptionRequests.ToArray();
              } else {
                // Policy violation error is not exemptable, remove this
                // operation from the list of operations.
                operationsToBeRemoved.Add(allOperations[index]);
              }
            } else {
              // This is not a policy violation error, remove this operation
              // from the list of operations.
              operationsToBeRemoved.Add(allOperations[index]);
            }
              }
              // Remove all operations that aren't exemptable.
              foreach (AdGroupAdOperation operation in operationsToBeRemoved) {
            allOperations.Remove(operation);
              }
            }

            if (allOperations.Count > 0) {
              // Perform the operations exemptible of a policy violation.
              service.RequestHeader.validateOnly = false;
              retVal = service.mutate(allOperations.ToArray());

              // Display the results.
              if (retVal != null && retVal.value != null && retVal.value.Length > 0) {
            foreach (AdGroupAd newAdGroupAd in retVal.value) {
              Console.WriteLine("New ad with id = \"{0}\" and displayUrl = \"{1}\" was created.",
                  newAdGroupAd.ad.id, newAdGroupAd.ad.displayUrl);
            }
              } else {
            Console.WriteLine("No ads were created.");
              }
            } else {
              Console.WriteLine("There are no ads to create after policy violation checks.");
            }
              } catch (Exception ex) {
            throw new System.ApplicationException("Failed to create ads.", ex);
              }
        }