Adding new users using the API Wednesday, November 14 2007
A question that came up this week on the forums was “how can I check to see if a user exists before creating it when using the Community Server API’s?”
There are no tricks here, and it’s quite simple, but many examples I have seen do not make it clear how to do this. Simply put, the Users.Create() method returns an enum called CreateUserStatus which tells you all you need to know. There is no need to check to existence of a username or even an email address. You are safer leaving this to the framework to handle for you as this will result in the least amount of trips to the data store.
Here is an example of how you would add a user:
User newUser = new User();
newUser.Username = username;
newUser.Password = password;
newUser.Email = email;
CreateUserStatus createStatus = Users.Create(newUser);
You can then use createStatus to check the success of the creation.
switch (createStatus)
{
case CreateUserStatus.Created:
// Do something
break;
case CreateUserStatus.DuplicateUsername:
// Username already exisits
break;
// etc. etc.
}
And that’s it!
P.S. This is my first blog post at 37,000 feet. I’m currently travelling 500 mph somewhere off the coast of Canada bound for Dallas. 5 hours down, 5 to go.








Great post, good to see more folks jumping on the CS tips bandwagon (especially employees of Telligent). So you're going to meet the team eh? That IS a long flight!