Search code examples
c#dotnetnuke

Get values from ArrayList in DnnUsers


I have following ArrayList ..

RoleController objRoleController = new RoleController();

ArrayList UserList = objRoleController.GetUsersByRoleName(PortalSettings.PortalId, "Client");

I just want to fetch UserId and Display Name from UserList..What should i do???


Solution

  • Assuming you have a view model that consists of just the UserID and DisplayName like this:

    public class UserViewModel
    {
        public int Id { get; set; }
        public string DisplayName { get; set; }
    }
    

    Then you can use one of two methods depending on what version of DNN you are using:

    public IEnumerable<UserViewModel> GetUsersBefore73()
    {
        var objRoleController = new RoleController();
        ArrayList UserList = objRoleController
            .GetUsersByRoleName(PortalSettings.PortalId, "Client");
    
        var users = from user in UserList.OfType<UserInfo>().ToList<UserInfo>()
            select new UserViewModel() {
                Id = user.UserID,
                DisplayName = user.DisplayName
            };
    
        return users;
    }
    

    In DNN 7.3 we deprecated the instance method and instead moved to using a factory to get the role controller. We also stopped using an ArrayList and instead started using List. For 7.3 and above you can use the following code:

    public IEnumerable<UserViewModel> GetUsersAfter73()
    {
        IList<UserInfo> UserList = RoleController
            .Instance
            .GetUsersByRole(PortalSettings.PortalId, "Client");
    
        var users = from user in UserList
            select new UserViewModel() {
                Id = user.UserID,
                DisplayName = user.DisplayName
            };
    
        return users;
    }