Export (0) Print
Expand All

Update Targets in Library in C#

The following example shows how to update an existing target within a customer’s library using the following Campaign Management service operations.

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 UpdateTarget(
    CampaignManagementServiceClient service,
    string username,
    string password,
    string appToken,
    string devToken,
    long accountId,
    long customerId,
    long targetId)
{
    // Get the existing target from the library.
    GetTargetsByIdsRequest getRequest = new GetTargetsByIdsRequest();
    getRequest.ApplicationToken = appToken;
    getRequest.CustomerAccountId = accountId.ToString();
    getRequest.CustomerId = customerId.ToString();
    getRequest.DeveloperToken = devToken;
    getRequest.UserName = username;
    getRequest.Password = password;

    getRequest.TargetIds = new long[1];
    getRequest.TargetIds[0] = targetId;

    GetTargetsByIdsResponse getResponse = null;

    try
    {
        getResponse = service.GetTargetsByIds(getRequest);
    }

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

        // Throw an application exception so that the update code below 
        // doesn't get called.
        throw new ApplicationException("AdApiFaultDetail Fault Received.");
    }

    // 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);
        }

        // Throw an application exception so the update code below doesn't 
        // get called.
        throw new ApplicationException("ApiFaultDetail Fault Received.");
    }

    /*
     * This example will add a gender target to the target if one does 
     * not exist. If a gender target does exist, the existing gender 
     * target will be modified to change the gender value.
     * */
    Target target = getResponse.Targets[0];

    /* The IsLibraryTarget element is read-only, so set it to null to avoid 
     * an error.
     */
    target.IsLibraryTarget = null;

    if (null == target.Gender)
    {
        target.Gender = new GenderTarget();
        target.Gender.Bids = new GenderTargetBid[1];
        target.Gender.Bids[0] = new GenderTargetBid();
        target.Gender.Bids[0].IncrementalBid = 
            IncrementalBidPercentage.FiftyPercent;
        target.Gender.Bids[0].Gender = GenderType.Female;
    }
    else
    {
        if (GenderType.Female == target.Gender.Bids[0].Gender)
        {
            target.Gender.Bids[0].Gender = GenderType.Male;
        }
        else
        {
            target.Gender.Bids[0].Gender = GenderType.Female;
        }
    }

    UpdateTargetsInLibraryRequest updateRequest = 
        new UpdateTargetsInLibraryRequest();
    updateRequest.ApplicationToken = appToken;
    updateRequest.CustomerAccountId = accountId.ToString();
    updateRequest.CustomerId = customerId.ToString();
    updateRequest.DeveloperToken = devToken;
    updateRequest.UserName = username;
    updateRequest.Password = password;

    updateRequest.Targets = getResponse.Targets;

    try
    {
        UpdateTargetsInLibraryResponse response =
            service.UpdateTargetsInLibrary(updateRequest);
    }

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