Creating a user account
Now that initialization is complete, the app can send a request to create
a new user account.
These sections explain the types of user accounts that you can create
using the BWS.createUsers API, and highlight the section of the code sample that is used
to create a new user account.
Specify the email address for the new user
The first step to create a new user is to specify the email address
to associate with the user.
/* * Email address used to create a new user with the createUsers() API call. * This value must exactly match the full string value in the directory for successful user creation. */ CreateNewUserEmail = "user02@example.net\";
Create the user
Next, you create a
CreateUsersRequest
object to send
the request to the BlackBerry Web
Services
, and assign the Metadata
object that you
previously created to its metadata
property. The
NewUser
object that represents the user and the
AccountAttributes
object contains information about the user,
like the email address// Create the request object. CreateUsersRequest createUsersRequest = new CreateUsersRequest(); createUsersRequest.metadata = Metadata; NewUser newUser = new NewUser(); // To create an administrator user, create and set the "UserAttributes" and the roleUid field. AccountAttributes accountAttributes = new AccountAttributes(); logMessage("Email address set to \"{0}\"", CreateNewUserEmail); // Value of the variable "CreateNewUserEmail" is used to create the user. accountAttributes.emailAddress = CreateNewUserEmail; newUser.accountAttributes = accountAttributes; // Server value is validated and then ignored. newUser.server = null;
Prepare and send the API call
Lastly, you prepare and send the request. The app adds the user to
the
CreateUsersRequest
, BWS.createUsersRequest()
is called to send the request, and the result of the request is verified and output
to the console.List<NewUser> newUsers = new List<NewUser>(); newUsers.Add(newUser); createUsersRequest.newUsers = newUsers.ToArray(); CreateUsersResponse response = null; try { logRequest(bwsApiName); 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}\"",individualResponse.uid); displayResult("Email address used in creation is \"{0}\"", accountAttributes.emailAddress); } returnValue = true; } } else { 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); } } }