Wednesday, 4 June 2014

Get user Roles list in DNN

It is a very common task in DNN, when we need to get users Roles. I am posting the different ways we have in DNN by which we can get userRoles.

1. Using this code you can get User List who belongs to particular role and show users from combo box:

 DotNetNuke.Security.Roles.RoleController objRoleController = new DotNetNuke.Security.Roles.RoleController();  
 ArrayList listroles = objRoleController.GetUsersByRoleName(PortalId, "Single Portal Admin");  
 List liUserInfo=new List();  
 foreach (object i in listroles)  
 {  
 DotNetNuke.Entities.Users.UserInfo objuser = (DotNetNuke.Entities.Users.UserInfo)i;  
 liUserInfo.Add(objuser);  
 }  
 drpuser.DataSource = liUserInfo;  
 drpuser.DataTextField = "Username";  
 drpuser.DataValueField = "UserID";  
 drpuser.DataBind();  
 drpuser.Items.Insert(0, "Select User");  
 drpuser.SelectedValue = "Select User";  


OR YOU CAN USE THE FOLLOWING CODE

private void BindRolesDropDownList()
{
   drpRoles.Items.Clear();
   var roles = _roleGroupId < -1
          ? TestableRoleController.Instance.GetRoles(PortalId)
 : TestableRoleController.Instance.GetRoles(PortalId, r => r.RoleGroupID == _roleGroupId);
      drpRoles.DataSource = roles;
      drpRoles.DataTextField = "RoleName";
      drpRoles.DataValueField = "RoleId";
      drpRoles.DataBind();
      drpRoles.Items.Insert(0, new ListItem("Select Role", "-1"));
}


2. How to get portal user

 DotNetNuke.Entities.Users.UserController objUserController = new DotNetNuke.Entities.Users.UserController();  
       ArrayList listroles = objUserController.GetUsers(PortalId, true, true);  
       List liUserInfo = new List();  
       foreach (object i in listroles)  
       {  
         DotNetNuke.Entities.Users.UserInfo objuser = (DotNetNuke.Entities.Users.UserInfo)i;  
         liUserInfo.Add(objuser);  
       }  
       drpuserlist.DataSource = liUserInfo;  
       drpuserlist.DataTextField = "Username";  
       drpuserlist.DataValueField = "UserID";  
       drpuserlist.DataBind();  


3. How to add user to perticular role

 SqlDataProvider objSqlDataProvider = new SqlDataProvider();  
         Guid objGuid=new Guid(hdngroupid.Value.ToString());  
         if (objSqlDataProvider.AddUserInGroup(objGuid,Convert.ToInt32(drpuserlist.SelectedValue)) > 0)  
         {  
           Lblerror.Text = "User added in Group!";  
           Lblerror.ForeColor = System.Drawing.Color.Green;  
           binduserlist(objGuid);  
         }  
         else  
         {  
           Lblerror.Text = "User exist in Group!";  
           Lblerror.ForeColor = System.Drawing.Color.Red;  
         }  


4. How to get roles by name

 RoleController ObjRoleController = new RoleController();  
         RoleInfo OBjRoleInfo = ObjRoleController.GetRoleByName(this.PortalId, drproledropdown.SelectedValue.ToString());  
         ObjRoleController.AddUserRole(this.PortalId, this.UserId, OBjRoleInfo.RoleID, DateTime.Now, DateTime.Now.AddYears(100));  
         string flag = drproledropdown.SelectedValue.ToString();  
         switch (flag)  
         {  
           case "Practice_Admin":  
             SessionManager.eH_ControlName = "SigningUpPractice.ascx";  
             break;  
           case "Patients":  
             SessionManager.eH_ControlName = "PatientRegistration.ascx";  
             break;  
    }   


5. Get All Roles in the Website:

 ArrayList list = new ArrayList();  
     DotNetNuke.Security.Roles.RoleController mUserRoles = new DotNetNuke.Security.Roles.RoleController();  
     list = mUserRoles.GetRoles();  
     drpRoles.DataSource = list;  
     drpRoles.DataTextField = "RoleName";  
     drpRoles.DataValueField = "RoleId";  
     drpRoles.DataBind();   

That's all