/// <summary>
    /// Runs the code example.
    /// </summary>
    /// <param name="user">The AdWords user.</param>
    public void Run(AdWordsUser user) {
      // Get the ExpressBusinessService.
      ExpressBusinessService businessService = (ExpressBusinessService)
          user.GetService(AdWordsService.v201402.ExpressBusinessService);

      ExpressBusiness business1 = new ExpressBusiness();
      business1.status = ExpressBusinessStatus.ACTIVE;
      business1.name = "Express Interplanetary Cruise #" + ExampleUtilities.GetShortRandomString();

      Address address1 = new Address();
      address1.streetAddress = "1600 Amphitheatre Pkwy";
      address1.cityName = "Mountain View";
      address1.provinceCode = "CA";
      address1.countryCode = "US";

      business1.address = address1;
      business1.website = "http://www.example.com/cruise1";

      ExpressBusinessOperation operation1 = new ExpressBusinessOperation();
      operation1.@operator = Operator.ADD;
      operation1.operand = business1;

      ExpressBusiness business2 = new ExpressBusiness();
      business2.status = (ExpressBusinessStatus.ACTIVE);
      business2.name = "Express Interplanetary Cruise #" + ExampleUtilities.GetShortRandomString();

      Address address2 = new Address();
      address2.streetAddress = "111 8th Ave";
      address2.cityName = "New York";
      address2.provinceCode = "NY";
      address2.countryCode = "US";

      business2.address = address2;
      business2.website = "http://www.example.com/cruise2";

      ExpressBusinessOperation operation2 = new ExpressBusinessOperation();
      operation2.@operator = Operator.ADD;
      operation2.operand = business2;

      try {
        ExpressBusiness[] addedBusinesses = businessService.mutate(
            new ExpressBusinessOperation[] {operation1, operation2});

        Console.WriteLine("Added {0} express businesses", addedBusinesses.Length);
        foreach (ExpressBusiness addedBusiness in addedBusinesses) {
          Console.WriteLine("Added express business with ID = {0} and name '{1}'.",
              addedBusiness.id, addedBusiness.name);
        }
      } catch (Exception ex) {
        throw new System.ApplicationException("Failed to add express business.", ex);
      }
    }
    /// <summary>
    /// Gets the geo location for a given address.
    /// </summary>
    /// <param name="user">The AdWords user.</param>
    /// <param name="address">The address for which geolocation should be
    /// fetched.</param>
    /// <returns>Geo location for the address.</returns>
    public GeoLocation GetLocationForAddress(AdWordsUser user, Address address) {
      GeoLocationService geoService =
          (GeoLocationService) user.GetService(AdWordsService.v201402.GeoLocationService);

      GeoLocationSelector selector = new GeoLocationSelector();
      selector.addresses = new Address[] {address};
      return geoService.get(selector)[0];
    }
    /// <summary>
    /// Formats the address as a printable text.
    /// </summary>
    /// <param name="address">The address object.</param>
    /// <returns>The formatted text.</returns>
    private static string FormatAddress(Address address) {
      if (address == null) {
        return "Not available.";
      }
      StringBuilder addressBuilder = new StringBuilder();

      addressBuilder.AppendFormat("Line 1: {0}\n", address.streetAddress ?? "");
      addressBuilder.AppendFormat("Line 2: {0}\n", address.streetAddress2 ?? "");
      addressBuilder.AppendFormat("Province Name: {0}\n", address.provinceName ?? "");
      addressBuilder.AppendFormat("Province Code: {0}\n", address.provinceCode ?? "");
      addressBuilder.AppendFormat("City name: {0}\n", address.cityName ?? "");
      addressBuilder.AppendFormat("Postal code: {0}\n", address.postalCode ?? "");
      addressBuilder.AppendFormat("Country name: {0}\n", address.countryCode ?? "");

      return addressBuilder.ToString();
    }
    /// <summary>
    /// Creates a campaign ad extension for running further tests.
    /// </summary>
    /// <param name="user">The AdWords user.</param>
    /// <param name="campaignId">The campaign id for which extension is
    /// created.</param>
    /// <returns>The campaign ad extension id.</returns>
    public long CreateLocationExtension(AdWordsUser user, long campaignId) {
      CampaignAdExtensionService campaignExtensionService =
          (CampaignAdExtensionService) user.GetService(AdWordsService.v201402.
               CampaignAdExtensionService);

      CampaignAdExtensionOperation operation = new CampaignAdExtensionOperation();
      operation.@operator = Operator.ADD;

      CampaignAdExtension extension = new CampaignAdExtension();
      extension.campaignId = campaignId;
      extension.status = CampaignAdExtensionStatus.ACTIVE;

      Address address = new Address();
      address.streetAddress = "1600 Amphitheatre Pkwy, Mountain View";
      address.countryCode = "US";

      GeoLocation location = GetLocationForAddress(user, address);

      LocationExtension locationExtension = new LocationExtension();

      // Note: Do not populate an address directly. Instead, use
      // GeoLocationService to obtain the location of an address,
      // and use the address as per the location it returns.
      locationExtension.address = location.address;
      locationExtension.geoPoint = location.geoPoint;
      locationExtension.encodedLocation = location.encodedLocation;
      locationExtension.source = LocationExtensionSource.ADWORDS_FRONTEND;

      extension.adExtension = locationExtension;
      operation.operand = extension;
      CampaignAdExtensionReturnValue retVal =
          campaignExtensionService.mutate(new CampaignAdExtensionOperation[] {operation});
      return retVal.value[0].adExtension.id;
    }
    /// <summary>
    /// Runs the code example.
    /// </summary>
    /// <param name="user">The AdWords user.</param>
    /// <param name="campaignId">Id of the campaign to which location
    /// extensions are added.</param>
    public void Run(AdWordsUser user, long campaignId) {
      // Get the CampaignAdExtensionService.
      CampaignAdExtensionService campaignExtensionService =
          (CampaignAdExtensionService) user.GetService(AdWordsService.v201402.
          CampaignAdExtensionService);

      // Add location 1: 1600 Amphitheatre Pkwy, Mountain View, US.
      Address address1 = new Address();
      address1.streetAddress = "1600 Amphitheatre Parkway";
      address1.cityName = "Mountain View";
      address1.provinceCode = "CA";
      address1.postalCode = "94043";
      address1.countryCode = "US";

      // Add location 2: 38 avenue de l'Opéra, 75002 Paris, FR.
      Address address2 = new Address();
      address2.streetAddress = "38 avenue de l'Opéra";
      address2.cityName = "Paris";
      address2.postalCode = "75002";
      address2.countryCode = "FR";

      // Get the GeoLocationService.
      GeoLocationService geoService =
          (GeoLocationService) user.GetService(AdWordsService.v201402.GeoLocationService);

      // Create the selector.
      GeoLocationSelector selector = new GeoLocationSelector();
      selector.addresses = new Address[] {address1, address2};

      // Retrieve the locations.
      GeoLocation[] locations = geoService.get(selector);

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

      // Phone numbers for US and FR offices.
      string[] phoneNumbers = new string[] {"(650) 253-0000", "(0)1 42 68 53 00"};
      int index = 0;

      // Create a location extension for each geo location returned by the
      // server.
      foreach (GeoLocation location in locations) {
        LocationExtension locationExtension = new LocationExtension();
        locationExtension.address = location.address;
        locationExtension.geoPoint = location.geoPoint;
        locationExtension.encodedLocation = location.encodedLocation;
        locationExtension.source = LocationExtensionSource.ADWORDS_FRONTEND;

        // Optional: Set the company name.
        locationExtension.companyName = "ACME Inc.";

        // Optional: Set the phone number.
        locationExtension.phoneNumber = phoneNumbers[index];
        index++;

        CampaignAdExtension extension = new CampaignAdExtension();
        extension.campaignId = campaignId;
        extension.status = CampaignAdExtensionStatus.ACTIVE;
        extension.adExtension = locationExtension;

        CampaignAdExtensionOperation operation = new CampaignAdExtensionOperation();
        operation.@operator = Operator.ADD;
        operation.operand = extension;

        operations.Add(operation);
      }

      try {
        CampaignAdExtensionReturnValue retVal =
            campaignExtensionService.mutate(operations.ToArray());

        // Display the results.
        if (retVal != null && retVal.value != null && retVal.value.Length > 0) {
          foreach (CampaignAdExtension campaignExtension in retVal.value) {
            Console.WriteLine("Created a location extension with id = \"{0}\" and " +
                "status = \"{1}\"", campaignExtension.adExtension.id, campaignExtension.status);
          }
        } else {
          Console.WriteLine("No location extensions were created.");
        }
      } catch (Exception ex) {
        throw new System.ApplicationException("Failed to add location extension.", ex);
      }
    }