Update Ad Groups in C#

The following example shows how to update ad groups within a campaign using the Campaign Management UpdateAdGroups 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 UpdateAdGroups(
    CampaignManagementServiceClient service,
    string username,
    string password,
    string appToken,
    string devToken,
    long customerAccountId,
    long customerId,
    long accountId,
    long campaignId,
    long adGroupId)
    UpdateAdGroupsRequest request = new UpdateAdGroupsRequest();
    request.ApplicationToken = appToken;
    request.CustomerAccountId = customerAccountId.ToString();
    request.CustomerId = customerId.ToString();
    request.DeveloperToken = devToken;
    request.Password = password;
    request.UserName = username;

    request.CampaignId = campaignId;

    request.AdGroups = new AdGroup[1];
    request.AdGroups[0] = new AdGroup();

    // The Id element specifies which ad group to update.
    request.AdGroups[0].Id = adGroupId;

    // Change the name.
    request.AdGroups[0].Name = "New ad group name";

    // Change the start date to tomorrow's date.
    request.AdGroups[0].StartDate = new Date();
    request.AdGroups[0].StartDate.Year = DateTime.Today.Year;
    request.AdGroups[0].StartDate.Month = DateTime.Today.Month;
    request.AdGroups[0].StartDate.Day = DateTime.Today.Day + 1;

    // Change the end date to one year from tomorrow.
    request.AdGroups[0].EndDate = new Date();
    request.AdGroups[0].EndDate.Year =
        request.AdGroups[0].StartDate.Year + 1;
    request.AdGroups[0].EndDate.Month =
    request.AdGroups[0].EndDate.Day =

        UpdateAdGroupsResponse response = service.UpdateAdGroups(request);

    // Exception handling.

    // 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.",

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

    // Capture any service operation exceptions.
    catch (FaultException<ApiFaultDetail> apiFault)
        ApiFaultDetail 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