Export (0) Print
Expand All

Update Customer in C#

The following example shows how get a customer and update some elements using the following Customer 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.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using UpdateCustomer.BingAds.CustomerManagement;

namespace UpdateCustomer
{
    class Program
    {
        private static CustomerManagementServiceClient service = null;

        // Specify your credentials.

        private static string m_userName = "<UserNameGoesHere>";
        private static string m_password = "<PasswordGoesHere>";
        private static string m_token = "<DeveloperTokenGoesHere>";

        // Specify the identifier of the customer to update.

        private const long customerId = <CustomerIdGoesHere>;

        static void Main(string[] args)
        {
            try
            {
                service = new CustomerManagementServiceClient("BasicHttpBinding_ICustomerManagementService");

                UpdateCustomer(m_customerId);

                service.Close();
            }
            catch (CommunicationException e)
            {
                Console.WriteLine(e.Message);

                if (service != null)
                {
                    service.Abort();
                }
            }
            catch (TimeoutException e)
            {
                Console.WriteLine(e.Message);

                if (service != null)
                {
                    service.Abort();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);

                if (service != null)
                {
                    service.Abort();
                }
            }
        }

        // Update the customer's data.
        // The update will fail if another user updated the
        // customer data between the time you retrieved the customer
        // object and the time you called the UpdateCustomer operation.

        static void UpdateCustomer(long customerId)
        {
            UpdateCustomerRequest request = new UpdateCustomerRequest();
            UpdateCustomerResponse response = null;
            Customer customer = null;

            customer = GetCustomerData(customerId);

            if (null != customer)
            {
                // You can update all elements
                // that are not marked as read-only on MSDN.
                // Update the fields as appropriate.
                // This example updates the customer’s address.

                customer.CustomerAddress.Line1 = "6789 Rollingstone";
                customer.CustomerAddress.City = "Redmond";
                customer.CustomerAddress.PostalCode = "98444";

                try
                {
                    request.UserName = m_username;
                    request.Password = m_password;
                    request.DeveloperToken = m_token;
                    request.Customer = customer;

                    response = service.UpdateCustomer(request);

                    Console.WriteLine("Customer {0} ({1}) was successfully updated.", customer.Name, customerId);
                }
                catch (FaultException<AdApiFaultDetail> fault)
                {
                    Console.WriteLine("The following errors occurred while updating customer, {0} ({1}).", customer.Name, customer.Id);

                    foreach (AdApiError error in fault.Detail.Errors)
                    {
                        Console.WriteLine("Error code: {1}\nMessage: {0}\nDetail: {2}\n", error.Message, error.Code, error.Detail);
                    }
                }
                catch (FaultException<ApiFault> fault)
                {
                    foreach (OperationError error in fault.Detail.OperationErrors)
                    {
                        if (1001 == error.Code)
                        {
                            // You cannot update the read-only
                            // fields, such as the status fields.

                            Console.WriteLine("You do not have permissions to update one or more of the fields.\n");
                        }
                        else if (510 == error.Code)
                        {
                            // You should consider retrieving
                            // the customer object again to get 
                            // the new time stamp and data.
                            // You can then show the latest
                            // updates to the user and confirm
                            // that they want to continue with
                            // their update. If so, update the
                            // elements as appropriate, and call
                            // the UpdateCustomer operation.

                            Console.WriteLine("Another user updated the customer.\n");
                        }
                        else
                        {
                            Console.WriteLine("The following error occurred trying to update customer {0}.\n", customerId);
                            Console.WriteLine("Error code: {1}\nMessage: {0}\nDetails: {2}\n", error.Message, error.Code, error.Details);
                        }
                    }
                }
            }
        }

        // Because the update operation is a complete overwrite,
        // you must first get the customer's current data, which
        // includes the time stamp of the previous write operation.

        static Customer GetCustomerData(long customerId)
        {
            GetCustomerRequest request = new GetCustomerRequest();
            GetCustomerResponse response = null;

            try
            {
                request.UserName = m_username;
                request.Password = m_password;
                request.DeveloperToken = m_token;
                request.CustomerId = customerId;

                response = service.GetCustomer(request);
            }
            catch (FaultException<AdApiFaultDetail> fault)
            {
                Console.WriteLine("The following error occurred trying to access customer {0}.\n", customerId);
                
                foreach (AdApiError error in fault.Detail.Errors)
                {
                    Console.WriteLine("Error code: {1}\nMessage: {0}\nDetail: {2}\n", error.Message, error.Code, error.Detail);
                }
            }
            catch (FaultException<ApiFault> fault)
            {
                foreach (OperationError error in fault.Detail.OperationErrors)
                {
                    if (1001 == error.Code)
                    {
                        Console.WriteLine("You do not have permissions to access customer {0}.", customerId);
                    }
                    else
                    {
                        Console.WriteLine("The following error occurred trying to access customer {0}.\n", customerId); 
                        Console.WriteLine("Error code: {1}\nMessage: {0}\nDetails: {2}\n", error.Message, error.Code, error.Details);
                    }
                }
            }

            return (null == response) ? null : response.Customer;
        }
    }
}

Community Additions

Show:
© 2014 Microsoft