Export (0) Print
Expand All

Delete User in C#

The following example shows how to delete a customer’s user with 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 DeleteUser.BingAds.CustomerManagement;

namespace DeleteUser
{
    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 user to delete.

        private static long m_userId = <UserIdGoesHere>; 

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

                DeleteUser(m_userId);

                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 user.
        // The delete will fail if another user updates the user data
        // between the time that you retrieved the user object and the
        // time that you called the DeleteUser operation.

        static void DeleteUser(long userId)
        {
            DeleteUserRequest request = new DeleteUserRequest();
            DeleteUserResponse response = null;
            User user = null;

            user = GetUserData(m_userId);

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

                    response = service.DeleteUser(request);
                    Console.WriteLine("User {0} ({1}) was successfully deleted.", user.UserName, m_userId);
                }
                catch (FaultException<AdApiFaultDetail> fault)
                {
                    Console.WriteLine("The following errors occurred while deleting user, {0} ({1}).", user.UserName, user.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 user {0} ({1}).", user.UserName, user.Id);

                    foreach (OperationError error in fault.Detail.OperationErrors)
                    {
                        if (1001 == error.Code)
                        {
                            Console.WriteLine("You do not have permissions to delete user {0} ({1}).", user.UserName, user.Id);
                        }
                        else if (510 == error.Code)
                        {
                            // You should consider retrieving the
                            // user 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 DeleteUser with the new time stamp.

                            Console.WriteLine("Another user updated the user before you could delete them.\n");
                        }
                        else if (1341 == error.Code)
                        {
                            // You cannot delete a user who is the
                            // primary user of an account.
                            // Before you can delete users, you must
                            // remove them as the primary user of the
                            // accounts. See the PrimaryUserId element
                            // of the Account object.

                            Console.WriteLine("You cannot delete the user because it is the primary user of one or more accounts.\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 associated with the user's data,
        // you must first get the user object.

        static User GetUserData(long userId)
        {
            GetUserRequest request = new GetUserRequest();
            GetUserResponse response = null;

            try
            {
                request.UserName = m_username;
                request.Password = m_password;
                request.DeveloperToken = m_token;
                request.UserId = userId;

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

                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)
                {
                    Console.WriteLine("The following error occurred trying to access user {0}.\n", userId);

                    if (1001 == error.Code)
                    {
                        Console.WriteLine("You do not have permissions to access user {0}.", userId);
                    }
                    else
                    {
                        Console.WriteLine("Error code: {1}\nMessage: {0}\nDetails: {2}\n", error.Message, error.Code, error.Details);
                    }
                }
            }

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

Community Additions

Show:
© 2014 Microsoft