Export (0) Print
Expand All

Delete Account in C#

The following example shows how to inactivate an account 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 DeleteAccount.BingAds.CustomerManagement;

namespace DeleteAccount
{
    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 account to delete.

        private static long m_accountId = 121674; 

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

                DeleteAccount(m_accountId);

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


        // Delete the specified account.
        // The delete will fail if another user updates the account
        // between the time that you retrieved the account object and
        // the time that you called the DeleteAccount operation.

        static void DeleteAccount(long accountId)
        {
            DeleteAccountRequest request = new DeleteAccountRequest();
            DeleteAccountResponse response = null;
            Account account = null;

            account = GetAccountData(m_accountId);

            if (null != account)
            {
                try
                {
                    request.UserName = m_username;
                    request.Password = m_password;
                    request.DeveloperToken = m_token;
                    request.AccountId = (long)account.Id;
                    request.TimeStamp = account.TimeStamp;

                    response = service.DeleteAccount(request);

                    Console.WriteLine("Account {0} ({1}) was successfully deleted.", account.Name, m_accountId);
                }
                catch (FaultException<AdApiFaultDetail> fault)
                {
                    Console.WriteLine("The following errors occurred while deleting account, {0} ({1}).", account.Name, account.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)
                {
                    Console.WriteLine("The following error occurred trying to delete account {0} ({1}).", account.Name, account.Id);

                    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 delete account, {0} ({1}).", account.Name, account.Id);
                        }
                        else if (112 == error.Code)
                        {
                            // Because the get operations will return
                            // accounts that have been deleted,
                            // you may want to confirm the account's
                            // status before trying to delete
                            // the account to avoid this error.

                            Console.WriteLine("The account has already been deleted.");
                        }
                        else if (510 == error.Code)
                        {
                            // You should consider retrieving the
                            // customer object again to get the
                            // new time stamp and data,
                            // show the latest updates to the user,
                            // and confirm that they want to
                            // continue with their delete.
                            // If so, call the DeleteCustomer
                            // operation with the new time stamp.

                            Console.WriteLine("Another user updated the account before you could delete it.\n");
                        }
                        else
                        {
                            Console.WriteLine("Error code: {1}\nMessage: {0}\nDetails: {2}\n", error.Message, error.Code, error.Details);
                        }
                    }
                }
            }
        }

        // Because the delete operation requires the time stamp
        // of the previous write operation against the account,
        // you must first get the account object.

        static Account GetAccountData(long accountId)
        {
            GetAccountRequest request = new GetAccountRequest();
            GetAccountResponse response = null;

            try
            {
                request.UserName = m_username;
                request.Password = m_password;
                request.DeveloperToken = m_token;
                request.AccountId = accountId;

                response = service.GetAccount(request);
            }
            catch (FaultException<AdApiFaultDetail> fault)
            {
                Console.WriteLine("The following error occurred trying to access account {0}.\n", accountId);

                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 account {0}.", accountId);
                    }
                    else
                    {
                        Console.WriteLine("The following error occurred trying to access account {0}.\n", accountId);
                        Console.WriteLine("Error code: {1}\nMessage: {0}\nDetails: {2}\n", error.Message, error.Code, error.Details);
                    }
                }
            }

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

Community Additions

Show:
© 2014 Microsoft