Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In kentico 10 we using bulk user upload from excel, the field name is Username, Email, fname, lname, Role1, Role2 and Role3.
The user will be insert working fine.
But, Roles are not insert. If I debug the code, the cursor not going to inside the if condition.

What I have tried:

private UserInfo CreateUser(UserDto userDto, SiteInfo site)
       {
           var newUser = new UserInfo
           {
               UserName = userDto.UserName,
               UserEnabled = true,
               FirstName = userDto.FirstName,
               LastName = userDto.LastName,
               FullName = userDto.FirstName + " " + userDto.LastName,
               Email = userDto.Email,
               SiteIndependentPrivilegeLevel = CMS.Base.UserPrivilegeLevelEnum.None
           };

           var newUserSettings = newUser.UserSettings ?? new UserSettingsInfo();
           UserInfoProvider.SetPassword(newUser, userDto.UserPassword);
           newUserSettings.UserPhone = userDto.PhoneNumber;

               //  var role = new RoleProvider().GetAllRoles(site.SiteID);
               // .FirstOrDefault(r => r.Description == userDto.Role1);
           UserInfoProvider.SetUserInfo(newUser);
           UserInfoProvider.AddUserToSite(newUser.UserName, site.SiteName);

          // var rolelist = userDto.Role1 ?? $"{userDto.Role2} {userDto.Role3}";

           var rolelist =  new[] { $"{userDto.Role1}", userDto.Role2, userDto.Role3}
                   .Where(af => !string.IsNullOrWhiteSpace(af));

           foreach (var u in rolelist)
           {
               List<string> userRoles = new List<string> { userDto.Role1, userDto.Role2, userDto.Role3 };
               foreach (string r in userRoles)
               {
                   var roles = RoleInfoProvider.GetRoles().WhereEquals("LOWER(RoleDescription)", r.ToLower()).FirstOrDefault();
                   if (userRoles != null)
                   {
                       UserInfoProvider.AddUserToRole(newUser.UserID, roles.RoleID);
                   }

           }
          }
           return newUser;


       }
Posted
Comments
Richard MacCutchan 3-Sep-19 5:02am    
You need to use the debugger to find out what is happening. Without your input data it is impossible to know why this fails.
Member 14557688 3-Sep-19 5:53am    
In the if (roles != null) roles variable null value is coming
Richard MacCutchan 3-Sep-19 6:07am    
Well that is fairly obvious, but it does not help to find out why. As i already suggested, you need to use the debugger to find out why. Step through the code and check the variables that are being used to populate the list, to see why none are being selected.
Richard Deeming 3-Sep-19 7:27am    
You don't have a if (roles != null) line in your code.

You have if (userRoles != null) - is that a typo in your question, or a typo in your code?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900