Adding new users using the API

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.

 

2 comment(s) so far

admin avatar

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!

admin avatar

Hi,

 I am using "Extended Properties" to fetch extra information about the user.

I want to display this value in grid.

In "controlpanel/Membership/MemberSearch.aspx" page,we have component grid displaying following information about all the users

a)Username b)email c)posts d)joined e) last active f) Action buttons.

Along with this coulmn info ,i want to display Extra(extended properties) information in separate coulmn in grid for all the users.

Say for example i have extended properties called "Department" .So i would like to show one more column called "Department" and under this  and display department name

for all the user if exists,(if not exists,just leave blank).

Is it possible to achieve it using this function <%=GetUserToView().GetExtendedAttribute("Department")%>

OR how to fecth the data from tables.I tried to fetch the data from cs_UserProfile table,It is not retrieve the values..?

THank u

Deepak

Post your comment