Portal Engine
Version 3.x > Portal Engine > Creating/Updating User View modes: 
User avatar
Member
Member
kpankratov@capprod.com - 8/15/2008 2:37:36 PM
   
Creating/Updating User
We have some problems while creating or updating users.

Here is a simple, test method to create new user.

-------------------------------------------------------------------------------------------------------------------------------------------------------------------
private string KenticoCreateUser()
{
// local variables
int userID = -1;

try
{
// init user entity in CMS
CMS.SiteProvider.UserInfo ui = new CMS.SiteProvider.UserInfo();

// set user values
ui.Email = "capuser@capprod.com";
ui.LastName = "caplast";
ui.FirstName = "capfirst";
ui.FullName = "capfull";
ui.UserName = "capuser@capprod.com";
ui.Enabled = true;
ui.IsEditor = false;
ui.IsGlobalAdministrator = false;

// create new user
CMS.SiteProvider.UserInfoProvider.SetUserInfo(ui);

// get user id
userID = ui.UserID;

// set user password
CMS.SiteProvider.UserInfoProvider.SetPassword(ui.UserName, "12345");

// set user sites
CMS.SiteProvider.UserInfoProvider.AddUserToSite(ui.UserName, "HQPortail");
// set user roles
CMS.SiteProvider.UserInfoProvider.AddUserToRole(ui.UserName, "HQMembres", "HQPortail");


}
catch
{
throw;
}

// return
return userID.ToString();
}
---------------------------------------------------------------------------------------------------------

The lines highlited in yellow, throw the following exception:

[TranslationHelper.GetObject]: Event OnGetObject is not initialized, cannot retrieve object code name column name


Once the method above is executed, a new user is created, I can see it in CMS Site Manager user’s list. But, no site is affected to this user and no roles are defined.

Second. If I do this:

try
{
// set user sites
CMS.SiteProvider.UserInfoProvider.AddUserToSite(ui.UserName, "HQPortail");
}
catch
{
}
try
{
// set user roles
CMS.SiteProvider.UserInfoProvider.AddUserToRole(ui.UserName, "HQMembres", "HQPortail");
}
catch
{
}
I just placed empty try catch blocks to let the code execute even if errors occurs. In that case, the new user is created and is added to sites and roles.


Please, let me know if I am doing something wrong.
Waiting for you help,
Thank you in advance

User avatar
Member
Member
kpankratov@capprod.com - 8/15/2008 2:40:30 PM
   
RE:Creating/Updating User
Lines that throw exception(highlited in yellow):

// set user sites
CMS.SiteProvider.UserInfoProvider.AddUserToSite(ui.UserName, "HQPortail");
// set user roles
CMS.SiteProvider.UserInfoProvider.AddUserToRole(ui.UserName, "HQMembres", "HQPortail");

User avatar
Kentico Developer
Kentico Developer
kentico_helenag - 8/21/2008 7:31:13 AM
   
RE:Creating/Updating User
We resolve this by the email, only for other visitors:
The code is correct, there could be a problem in the enviroment where it is used.

Best Regards,
Helena Grulichova

User avatar
Member
Member
dukebaby - 4/20/2009 2:04:54 PM
   
RE:Creating/Updating User
I am getting the same exception and it also appears to be with the AddUserToSite() method.


User avatar
Kentico Developer
Kentico Developer
kentico_helenag - 4/28/2009 3:30:56 AM
   
RE:Creating/Updating User
Hello,

1. Could you please check the site name? If I set the site name which does not exist it affects only global list of users. In the case the name was right it creates the user in my site with appropriate role.

2. Could you please try to use this code in some other place? For example I was testing this in the 'OnAfterSave' method in the BizForm.

Best regards,
Helena Grulichova


User avatar
Certified Developer v7
Certified  Developer v7
bsingh-visualantidote - 10/21/2013 11:51:57 AM
   
RE:Creating/Updating User
Hi ,

I think you should check if sitname / rolename is correct.

Code below works for me:


UserInfo usr=UserInfoProvider.GetUserInfo(username);

string siteName = CMSContext.CurrentSite.SiteName;


// Assign user to the current site
if (!usr.IsInSite(siteName))
{
UserInfoProvider.AddUserToSite(usr.UserName, siteName);
}


///check if role exists
if (CMS.SiteProvider.RoleInfoProvider.RoleExists(_rolecodename, CMSContext.CurrentSite.SiteName))
{
/// if exists then assign role to user
AssignRoleToUser(user.UserID, _rolecodename);
}

/// assign role to user
private void AssignRoleToUser(int usrid, string rolecodenm)
{
RoleInfo _role = RoleInfoProvider.GetRoleInfo(rolecodenm, CMSContext.CurrentSiteID);
UserInfo userinfo = UserInfoProvider.GetUserInfo(usrid);
if (!userinfo.IsInRole(rolecodenm, CMSContext.CurrentSiteName))
{
// UserInfoProvider.AddUserToRole(userinfo.UserName, _role.RoleID);
UserInfoProvider.AddUserToRole(userinfo.UserName, _role.RoleName, CMSContext.CurrentSiteName);

}

}


Regards,
Balinder