Update Ads in C#

The following example shows how to update ads within an ad group using the Campaign Management UpdateAds service operation.

This example has been developed and run within the environment described in Getting Started Using C# and Visual Basic with Bing Ads Services.

System_CLiX_note Note

This example uses the UserName and Password elements for authentication. For Managing User Authentication with OAuth, replace the UserName and Password elements with the AuthenticationToken, which is your OAuth access token.

static void UpdateAds(
    CampaignManagementServiceClient service,
    string username,
    string password,
    string appToken,
    string devToken,
    long customerAccountId,
    long customerId,
    long adGroupId,
    long adId)
    UpdateAdsRequest request = new UpdateAdsRequest();
    request.ApplicationToken = appToken;
    request.CustomerAccountId = customerAccountId.ToString();
    request.CustomerId = customerId.ToString();
    request.DeveloperToken = devToken;
    request.Password = password;
    request.UserName = username;

    // Set the ad group identifier.
    request.AdGroupId = adGroupId;

    request.Ads = new Ad[1];

    // Initialize the new text ad.
    TextAd textAd1 = new TextAd();
    textAd1.Id = adId;
    textAd1.Text = "New ad text";
    textAd1.DisplayUrl = "http://www.alpineskihouse.com";

    request.Ads[0] = textAd1 as Ad;

        UpdateAdsResponse response = service.UpdateAds(request);

        // Handle any faults from the Web service.
    // Exceptions not handled here will be passed back to the 
    // caller.

    // Capture any generic errors.
    catch (FaultException<AdApiFaultDetail> adApiFault)
        // Get the AdApiFaultDetail object.
        AdApiFaultDetail detail = adApiFault.Detail;
        foreach (AdApiError error in detail.Errors)
            Console.Write("Ad API error");
            Console.WriteLine(" '{0}' ({1}) encountered.",

    // Capture any service operation exceptions.
    catch (FaultException<EditorialApiFaultDetail> apiFault)
        EditorialApiFaultDetail detail = apiFault.Detail;

        // Display any service operation error information.
        foreach (OperationError opError in detail.OperationErrors)
            Console.Write("Operation error");
            Console.WriteLine(" '{0}' ({1}) encountered.",

        // Display any batch error information.
        foreach (BatchError batchError in detail.BatchErrors)
            Console.Write("Batch error");
            Console.Write(" '{0}' ({1}) encountered",
            Console.WriteLine(" array index = {0}.",


Community Additions