Export (0) Print
Expand All

Target Ad Groups in C#

The following examples show how to add a target to a customer’s library, and assign the target to an ad group using the following Campaign Management service operations. The first code snippet shows how to add a target to your library. The second code snippet shows how to assign a library target to an ad group.

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 long AddAgeTarget(
    CampaignManagementServiceClient service,
    string username,
    string password,
    string appToken,
    string devToken,
    long accountId,
    long customerId)
{
    AddTargetsToLibraryRequest request = new AddTargetsToLibraryRequest();
    request.ApplicationToken = appToken;
    request.CustomerAccountId = accountId.ToString();
    request.CustomerId = customerId.ToString();
    request.DeveloperToken = devToken;
    request.UserName = username;
    request.Password = password;

    AgeTargetBid ageTargetBid = new AgeTargetBid();
    ageTargetBid.Age = AgeRange.ThirtyFiveToFifty;
    ageTargetBid.IncrementalBid = IncrementalBidPercentage.FortyPercent;

    AgeTarget ageTarget = new AgeTarget();
    ageTarget.Bids = new AgeTargetBid[1];
    ageTarget.Bids[0] = ageTargetBid;

    Target[] targets = new Target[1];
    targets[0] = new Target();
    targets[0].Age = ageTarget;
    targets[0].Name = "Age Target Name";

    request.Targets = targets;

    long targetId = 0;

    try
    {
        AddTargetsToLibraryResponse response = 
            service.AddTargetsToLibrary(request);

        targetId = response.TargetIds[0];

        foreach (long id in response.TargetIds)
        {
            Console.WriteLine(String.Format("New target ID: {0}", id));
        }
    }

    // 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.",
                error.Message,
                error.Code);
        }
    }

    // Capture API exceptions.
    catch (FaultException<ApiFaultDetail> fault)
    {
        ApiFaultDetail detail = fault.Detail;

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

        // Display any batch errors.
        foreach (BatchError batchError in detail.BatchErrors)
        {
            Console.Write("Batch error");
            Console.WriteLine(" '{0}' ({1}) encountered for item {2}.",
                batchError.Message,
                batchError.Code,
                batchError.Index);
        }
    }

    return targetId;
}

static void SetTargetToAdGroup(
    CampaignManagementServiceClient service,
    string username,
    string password,
    string appToken,
    string devToken,
    long accountId,
    long customerId,
    long targetId,
    long adGroupId)
{
    SetTargetToAdGroupRequest request = new SetTargetToAdGroupRequest();
    request.ApplicationToken = appToken;
    request.CustomerAccountId = accountId.ToString();
    request.CustomerId = customerId.ToString();
    request.DeveloperToken = devToken;
    request.UserName = username;
    request.Password = password;

    request.AdGroupId = adGroupId;
    request.TargetId = targetId;

    try
    {
        SetTargetToAdGroupResponse response =
            service.SetTargetToAdGroup(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.",
                error.Message,
                error.Code);
        }
    }

    // Capture API exceptions.
    catch (FaultException<ApiFaultDetail> fault)
    {
        ApiFaultDetail detail = fault.Detail;

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

        // Display any batch errors.
        foreach (BatchError batchError in detail.BatchErrors)
        {
            Console.Write("Batch error");
            Console.WriteLine(" '{0}' ({1}) encountered for item {2}.",
                batchError.Message,
                batchError.Code,
                batchError.Index);
        }
    }
}

Community Additions

Show:
© 2014 Microsoft