How to assign site for new user via Kentico API

124 Views Asked by At

I'm trying to create a new Kentico user and assigning them to a site, via Kentico API: https://docs.kentico.com/api11/configuration/users#Users-Assigningausertoasite

In my case SiteContext.CurrentSiteName returns empty string. I thought of trying by hard coding the site.

UserInfor newUser = new UserInfo();
//...set all required properties...

//add user to site
UserInfoProvider.AddUserToSite(newUser.UserName, "My Site Code(or Display) Name");

//save user
UserInfoProvider.SetUserInfo(newUser);

Tried both Display Name as well as Code Name of the site in UserInfoProvider.AddUserToSite, but the user is not getting assigned to the site on Kentico site. It does get created with all the properties.

2

There are 2 best solutions below

0
On

Just had to add user to the site after saving the user.

//save user
UserInfoProvider.SetUserInfo(newUser);

//add user to site
UserInfoProvider.AddUserToSite(newUser.UserName, "My Site Code Name (not display name)");
0
On

You have to use SiteInfoProvider to get the site code name

var si = SiteInfoProvider.GetSites()
                         .WhereEquals("SiteId",123)
                         .FirstOrDefault();
UserInfoProvider.AddUserToSite(newUser.UserName, si.SiteName);