Export (0) Print
Expand All

Add User in C#

The following example shows how to add a user within a customer using the Customer Management AddUser service operation.

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 AddUser.BingAds.CustomerManagement;

namespace AddUser
{
    class Program
    {
        private static CustomerManagementServiceClient customerService = null;

        // Specify your credentials.

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

        // Specify the identifier of the customer to add the user to.

        private static long m_customerId = <CustomerIdGoesHere>;

        static void Main(string[] args)
        {
            long userId = -1;

            try
            {
                customerService = new CustomerManagementServiceClient("BasicHttpBinding_ICustomerManagementService");

                userId = AddUserToCustomer(m_customerId);

                if (userId > 0)
                {
                    Console.WriteLine("Added user {0}", userId);
                }

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

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

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

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

        // Add a user to the specified customer.

        static private long AddUserToCustomer(long customerId)
        {
            AddUserRequest request = new AddUserRequest();
            AddUserResponse response = null;
            User user = null;
            long[] accountIds = null;

            user = GetUserData();
            accountIds = GetAccountRestrictions();

            try
            {
                // If you do not specify Role, the default is the
                // AdvertiserCampaignManager account role. If you do
                // not specify a list of account identifiers, the user
                // will have AdvertiserCampaignManager permission to
                // all accounts that the customer manages.

                // You can set Role to the SuperAdmin customer role;
                // however, to limit the user to a subset of the
                // customers that the reseller manages,
                // you must call the UpdateUserRoles operation.

                request.UserName = userName;
                request.Password = password;
                request.DeveloperToken = token;
                request.User = user;
                request.AccountIds = accountIds;
                request.Role = UserRole.AdvertiserCampaignManager;
                request.User.CustomerId = customerId;

                response = customerService.AddUser(request);
            }
            catch (FaultException<AdApiFaultDetail> fault)
            {
                Console.WriteLine("AddUser failed with the following errors:");

                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("AddUser failed with the following errors:");

                foreach (OperationError error in fault.Detail.OperationErrors)
                {
                    if (1339 == error.Code)
                    {
                        Console.WriteLine("A user with username {0} already exists.", request.User.UserName);
                    }
                    else
                    {
                        Console.WriteLine("Error code: {1}\nMessage: {0}\nDetails: {2}\n", error.Message, error.Code, error.Details);
                    }
                }
            }

            return (null != response) ? response.Id : -1;
        }

        // Get the information of the user to add.

        static private User GetUserData()
        {
            User user = new User();
            ContactInfo contact = new ContactInfo();
            Address address = new Address();
            PersonName name = new PersonName();

            address.Line1 = "1234 Washington Place";
            address.Line2 = "Suite 1210";
            address.City = "Woodinville";
            address.StateOrProvince = "WA";
            address.PostalCode = "98068";
            address.CountryCode = "US";
            contact.Address = address;

            contact.ContactByPhone = true;
            contact.Phone1 = "(206) 555-0100 ext. 1234";
            contact.Email = "kim@alpineskihouse.com";
            contact.EmailFormat = EmailFormat.Text;

            user.ContactInfo = contact;

            user.CustomerAppScope = ApplicationType.Advertiser;

            name.FirstName = "Kim";
            name.LastName = "Abercrombie";
            user.Name = name;

            user.Password = "<PasswordGoesHere>";
            user.SecretQuestion = SecretQuestion.FatherMiddleName;
            user.SecretAnswer = "Michael";
            user.UserName = "kaberc";

            return user;
        }

        // Get a list of the accounts that the user is restricted to.
        // If the user has access to all accounts, return null.
        // This example limits the user to two accounts.

        static private long[] GetAccountRestrictions()
        {
            long[] accountIds = new long[] { <AccountIdGoesHere>, <AccountIdGoesHere> };

            return accountIds;
        }
    }
}

Community Additions

Show:
© 2014 Microsoft