Developer forum

Forum » Development » AccessUserSortXML in dw19.2.1.3

AccessUserSortXML in dw19.2.1.3


Reply

I am converting a module using the old HR module, to just using the Extranet. Previously i used this function to read all the users in a group:

private List GetUsersInGroup(int groupid)
    {
       
        List users = new List();
       
        string sql = "select * from [HRAccessUser] ";
        sql += "left join [AccessUser] on HRAccessUser.HRAccessUserID = AccessUser.AccessUserID ";
        sql += "where AccessUserActive = " + Database.SqlBool(true) + " ";
        sql += "order by AccessUser.AccessUserSort ";

        using (IDataReader dr = Database.getDataReader(sql, "Access.mdb"))
        {
            while (dr.Read())
            {
                if (dr["AccessUserGroups"].ToString().Contains("@" + groupid + "@"))
                {
                    users.Add(int.Parse(dr["AccessUserID"].ToString()));
                }
            }
        }

        return users;
    }

This won't work anymore, since the "AccessUserSort" is replaced by xml in the "AccessUserSortXML" field. So how do i read a collection of users sorted correctly in the new version?


Replies

 
Reply

Try please next code:

 

            Group targetGroup = Group.GetGroupByID(groupid);

            string sql = "select * from [HRAccessUser] ";

            sql += "left join [AccessUser] on HRAccessUser.HRAccessUserID = AccessUser.AccessUserID ";

            sql += "where AccessUserActive = " + Database.SqlBool(true) + " ";

            sql += string.Format("and [AccessUserGroups] LIKE '%@{0}@%' ", groupid);

            UserCollection users = User.GetUsersBySql(sql);           

            if (users.Count > 0)

            {

                users.SortBy(Dynamicweb.Modules.UserManagement.User.SortFields.Sorting, true, targetGroup);

            }

 

1.      I have modified your SQL and added checking users group to the SQL.

2.  Add required namespace: using Dynamicweb.Modules.UserManagement;

3.  Use SortBy method of UserCollection class. It takes tree parameter:

a.  Sort by. It can be Name, Username or sort order.

b.  Ascending or descending

c.  User group. Sort order can be different in different groups, so you have to exactly define sort order in what group do you need.

 
Reply
 That looks like exactly what i was looking for. Thank you very much! I'll try it out

 

You must be logged in to post in the forum