Optionally provide private feedback to help us improve this article...

Thank you for your feedback!


Creating Users & Adding To Custom Groups

This article shows how to create a user using the InstantForum .NET API and associate that user with a custom member group. This can be helpful if you wish to ensure users are associated with a specific group or number of groups at the time the account is created.

Creating a User & Adding a Single Role

// get all roles
var roles = InstantASP.Common.Business.Roles.SelectRoles();

// get User Group 1 - already created via the InstantForum Admin CP
InstantASP.Common.Components.RoleInfo userGroup1 = null;
if (roles != null)
{
userGroup1 = roles.SingleOrDefault(r =>  r.RoleName == "User Group 1");
}

// add a new user and associate the user with our user group 1 role
if (userGroup1 != null)
{

var user = new InstantASP.InstantForum.Components.User()
{
Username = "User 1",
EmailAddress = "user1@user1.com",
PrimaryRoleID = userGroup1.RoleID,
Password = "password123"
};

var userId = InstantASP.InstantForum.Business.User.InsertUpdateUser(user);
if (userId > 0)
{
// user was added OK
}
else
{
if (userId == -1)
{
// username already exists
}
else if (userId == -2)
{
// email address already exists
}
}

}

Creating a User & Adding Multiple Roles

// get User Group 1 - already created via the Admin CP
var roles = InstantASP.Common.Business.Roles.SelectRoles();

// get user group 1 role
InstantASP.Common.Components.RoleInfo primarRole = null;
InstantASP.Common.Components.RoleInfo secondaryRole1 = null;
InstantASP.Common.Components.RoleInfo secondaryRole2 = null;

if (roles != null)
{
primarRole = InstantASP.Common.Business.Roles.SelectRole(EnumRequiredRoles.Member);
secondaryRole1 = roles.SingleOrDefault(r => r.RoleName == "Role 1");
secondaryRole2 = roles.SingleOrDefault(r => r.RoleName == "Role 2");
}

// add a new user and associate them with our user group 1 role
if (primarRole != null)
{

var user = new InstantASP.InstantForum.Components.User()
{
Username = "User 1",
EmailAddress = "user1@user1.com",
PrimaryRoleID = primarRole.RoleID,
Password = "password123"
};

var userId = InstantASP.InstantForum.Business.User.InsertUpdateUser(user);
if (userId > 0)
{
// user was added OK

// create secondary roles
if (secondaryRole1 != null)
InstantASP.Common.Business.UserRoles.AddUserRole(userId, secondaryRole1.RoleID);
if (secondaryRole2 != null)
InstantASP.Common.Business.UserRoles.AddUserRole(userId, secondaryRole2.RoleID);

}
else
{
if (userId == -1)
{
// username already exists
}
else if (userId == -2)
{
// email address already exists
}
}

}

Updating an Existing Users Primary Role

// attempt to populate user object from database
var user = new InstantASP.InstantForum.Components.User("user1@user1.com");
// did we find a user witin the database with email user1@user1.com
if (user.UserID > 0)
{
// update the users primary role
user.PrimaryRoleID = 123;

// save the changes 
InstantASP.InstantForum.Business.User.InsertUpdateUser(user);

}

Updating an Existing Users Secondary Roles

// attempt to populate user object from database
var user = new InstantASP.InstantForum.Components.User("user1@user1.com");
// did we find a user witin the database with email user1@user1.com
if (user.UserID > 0)
{
// first delete all existing sedcondary roles for the user
InstantASP.Common.Business.UserRoles.DeleteUserRoles(user.UserID);

// add our secondary roles again - for the purpose of this example
// we'll just provide an hardcoded RoleID parameter - valid RoleID
// values can be found by looking within your InstantASP_Roles table.
InstantASP.Common.Business.UserRoles.AddUserRole(user.UserID, 2);
InstantASP.Common.Business.UserRoles.AddUserRole(user.UserID, 3);
InstantASP.Common.Business.UserRoles.AddUserRole(user.UserID, 4);
InstantASP.Common.Business.UserRoles.AddUserRole(user.UserID, 5);
}