Page 1 of 1

Search for Users on UserSettings

Posted: Wed Mar 25, 2015 4:55 am
by mbartens
I'd need to search for users based on their User Settings (ac_UserSettings)

Ideally it would search directly on that.

However since the settings are primarily Yes/No types I could modify my code so that when the user setting is saved it adds them to a group.
If I were going to do that though I would need to search multiple groups at one time.
So, would turning the Group dropdown into a checkbox list handle searching on multiple groups? Thanks!

Re: Search for Users on UserSettings

Posted: Thu Mar 26, 2015 1:46 am
by nadeem
I think you may try using a multiple-selection list box instead of check-box list. You can have a look at 'Change Assigned Groups' dialog on edit user page for example.

EDIT: Currently, the user search only supported against a single group. Therefore, you have to update back end logic to search against multiple groups to achieve the desired result.

Re: Search for Users on UserSettings

Posted: Fri Mar 27, 2015 4:38 am
by mbartens
Is there any example I can follow that can do a multi group search? Thanks so much.

Re: Search for Users on UserSettings

Posted: Fri Mar 27, 2015 4:55 am
by nadeem
Multi group search is not supported at the moment. You have to modify the code to work it against multi group.

Re: Search for Users on UserSettings

Posted: Fri Mar 27, 2015 6:15 am
by nadeem
Here is an example code for you. Open CommerceBuilder/Users/UserSearchCriteria.cs and made the following updates:

Include the following using statement at the top

Code: Select all

using System.Collections.Generic;
Add a new property to hold list of group ids

Code: Select all

/// <summary>
/// Gets or sets the groups of the user to search for
/// </summary>
public List<int> GroupIds { get; set; }
Add the GroupIds in the constructor to avoid null exception, something like this:

Code: Select all

/// <summary>
/// Initializes a new instance of the UserSearchCriteria class.
/// </summary>
public UserSearchCriteria()
{
   // DEFAULT VALUE
   this.ShowAssignedToGroup = true;
   this.GroupIds = new List<int>();
}
Now go to internal ICriteria GenerateCriteria(bool isCount, string sortExpression) method

Locate the following code:

Code: Select all

// BUILD THE WHERE CRITERIA
            if (this.GroupId > 0)
            {
                if (this.ShowAssignedToGroup)
                {
                    criteria.CreateCriteria("UserGroups", "UG", NHibernate.SqlCommand.JoinType.InnerJoin)
                    .Add(Restrictions.Eq("UG.GroupId", this.GroupId));
                }
                else
                {
                    DetachedCriteria subQuery = DetachedCriteria.For<UserGroup>("UG")
                        .Add(Restrictions.Eq("UG.GroupId", this.GroupId))
                        .SetProjection(Projections.Property("UG.UserId"));
                    criteria.Add(Subqueries.PropertyNotIn("U.Id", subQuery));
                }
            }
and replace with:

Code: Select all

// BUILD THE WHERE CRITERIA
            if (this.GroupId > 0)
            {
                if (this.ShowAssignedToGroup)
                {
                    criteria.CreateCriteria("UserGroups", "UG", NHibernate.SqlCommand.JoinType.InnerJoin)
                    .Add(Restrictions.Eq("UG.GroupId", this.GroupId));
                }
                else
                {
                    DetachedCriteria subQuery = DetachedCriteria.For<UserGroup>("UG")
                        .Add(Restrictions.Eq("UG.GroupId", this.GroupId))
                        .SetProjection(Projections.Property("UG.UserId"));
                    criteria.Add(Subqueries.PropertyNotIn("U.Id", subQuery));
                }
            }
            else if(GroupIds.Count > 0)
            {
                if (this.ShowAssignedToGroup)
                {
                    criteria.CreateCriteria("UserGroups", "UG", NHibernate.SqlCommand.JoinType.InnerJoin)
                    .Add(Restrictions.In("UG.GroupId", this.GroupIds.ToArray()));
                }
                else
                {
                    DetachedCriteria subQuery = DetachedCriteria.For<UserGroup>("UG")
                        .Add(Restrictions.In("UG.GroupId", this.GroupIds.ToArray()))
                        .SetProjection(Projections.Property("UG.UserId"));
                    criteria.Add(Subqueries.PropertyNotIn("U.Id", subQuery));
                }
            }
For testing I have set the groupIds from Admin/People/Users/Default.aspx.cs like this:

Code: Select all

criteria.GroupIds.AddRange(new List<int>() { 1, 2, 3, 4, 5 });
You have to take selected ids from multi select box.

Hope it helps.

Re: Search for Users on UserSettings

Posted: Tue Mar 31, 2015 3:12 am
by mbartens
This is working great - thank you!