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

Run() public method

Runs the code example.
public Run ( Google.Api.Ads.AdWords.Lib.AdWordsUser user, long adGroupId, long adId ) : void
user Google.Api.Ads.AdWords.Lib.AdWordsUser The AdWords user.
adGroupId long Id of the ad group that contains the ad.
adId long Id of the ad being deleted.
return void
        public void Run(AdWordsUser user, long adGroupId, long adId)
        {
            // Get the AdGroupAdService.
              AdGroupAdService adGroupAdService = (AdGroupAdService) user.GetService(
              AdWordsService.v201306.AdGroupAdService);

              // Since we do not need to update any ad-specific fields, it is enough to
              // create the base type.
              Ad ad = new Ad();
              ad.id = adId;

              // Create the ad group ad.
              AdGroupAd adGroupAd = new AdGroupAd();
              adGroupAd.adGroupId = adGroupId;

              adGroupAd.ad = ad;

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

              try {
            // Delete the ad.
            AdGroupAdReturnValue retVal = adGroupAdService.mutate(
            new AdGroupAdOperation[] {operation});

            if (retVal != null && retVal.value != null && retVal.value.Length > 0) {
              AdGroupAd deletedAdGroupAd = retVal.value[0];
              Console.WriteLine("Ad with id = \"{0}\" and type = \"{1}\" was deleted.",
              deletedAdGroupAd.ad.id, deletedAdGroupAd.ad.AdType);
            } else {
              Console.WriteLine("No ads were deleted.");
            }
              } catch (Exception ex) {
            throw new System.ApplicationException("Failed to delete 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)
 {
     DeleteAd codeExample = new DeleteAd();
       Console.WriteLine(codeExample.Description);
       try {
     long adGroupId = long.Parse("INSERT_ADGROUP_ID_HERE");
     long adId = long.Parse("INSERT_AD_ID_HERE");
     codeExample.Run(new AdWordsUser(), adGroupId, adId);
       } catch (Exception ex) {
     Console.WriteLine("An exception occurred while running this code example. {0}",
     ExampleUtilities.FormatException(ex));
       }
 }
DeleteAd