Code sample: Creating a user account

Visit to copy the full code sample to your development tool. This topic highlights and explains key sections of the code that are used to create a new BlackBerry-enabled user account. This topic refers to lines 516 to 601 and the main method of the code sample.

Before you run the code sample, verify that you have completed the configuration tasks described earlier in this guide. Also, verify that your project includes the System.Web.Services reference.

Specify the email address for the new user

The following code from the main method defines the CreateNewUserEmail variable that will be used to create the new user account. Specify the email address of the user account that you want the application to create.

// Email address used to create a new user with the createUsers() API call.
CreateNewUserEmail = "\"\"";

Create the CreateUsersRequest object

The following code creates the CreateUsersRequest object that is used to send the API call to the BlackBerry Web Services, and assigns the value of the Metadata global variable (see Code sample: Authentication and initializing the web services) to the metadata property of the CreateUsersRequest object.

// Create the request object.
CreateUsersRequest createUsersRequest = new CreateUsersRequest();
createUsersRequest.metadata = Metadata;

Create the NewUser object

The following code creates a NewUser object to hold the values of the account attributes for the user account.

NewUser newUser = new NewUser();

Create the AccountAttributes object

The following code creates an AccountAttributes object, which is used to set the value of the email address for the user account. The global variable CreateNewUserEmail contains the email address value.

// To create an administrator user, create and set the "UserAttributes".
AccountAttributes accountAttributes = new AccountAttributes();

* Check if value of the variable "CreateNewUserEmail" is enclosed in double-quotes,
* otherwise the string would infer a substring match search.
if (!CreateNewUserEmail.StartsWith("\"") || !CreateNewUserEmail.EndsWith("\""))
	logMessage("Warning: Email Address \"{0}\" is not enclosed in double-quotes",
// Value of the variable "CreateNewUserEmail" is used to create a BlackBerry-enabled user.
logMessage("Creating a user with email address \"{0}\"", CreateNewUserEmail);
accountAttributes.emailAddress = CreateNewUserEmail;

Set the attributes of the user account

The following code uses the NewUser object (called newUser) with the AccountAttribute object (called accountAttributes) to set the attributes of the user account. In this code sample, a null value is assigned to the newUser.server property to randomly select the BlackBerry Device Service to create the user on.

newUser.accountAttributes = accountAttributes;
// Randomly select a BlackBerry Device Service on which to create the user.
newUser.server = null;

Prepare the API call

The following code creates a newUsers List variable to hold the newUser object. The sample code assigns the newUser object to the createUsersRequest.newUsers property. The createUsersRequest object is ready to call the createUsers API.

List<NewUser> newUsers = new List<NewUser>();
createUsersRequest.newUsers = newUsers.ToArray();

Send the API call and verify that the API call was successful

The following code calls the createUsers API using bwsService.createUsers(createUsersRequest) and stores the response from the API call in a CreateUsersResponse object called response. The code checks the return status message stored in the CreateUsersResponse object to verify that the call to the API was successful. If the call was successful, the code iterates through the individual responses and displays the results. Otherwise, it displays any errors that were found in the individual responses.

CreateUsersResponse response = null;

	response = bwsService.createUsers(createUsersRequest);
	logResponse(bwsApiName, response.returnStatus.code, response.metadata);
catch (WebException e)
	// Log and re-throw exception.
	logMessage("Exiting {0} with exception \"{1}\"", methodName, e.Message);
	throw e;

if (response.returnStatus.code.Equals("SUCCESS"))
	if (response.individualResponses != null)
		foreach (IndividualResponse individualResponse in response.individualResponses)
			displayResult("User created with UID \"{0}\" using Email Address \"{1}\"", individualResponse.uid, accountAttributes.emailAddress);

		returnValue = true;
	logMessage("Error Message: \"{0}\"", response.returnStatus.message);
	if (response.individualResponses != null)
		foreach (IndividualResponse individualResponse in response.individualResponses)
			logMessage("Individual Response - Code: \"{0}\", Message: \"{1}\"",
				individualResponse.returnStatus.code, individualResponse.returnStatus.message);

Was this information helpful? Send us your comments.