Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

Get visitor groups for the current visitor

Vote:
 

Hello!

Is there a way to get in code all visitor groups that the current visitor may be part of while they are browsing the site? I found only how to get all visitor groups - using visitorGroupRepository.List().

Thank you,

Simona

#188240
Feb 15, 2018 12:27
Vote:
 

I think you can try something like this:

var visitorGroupRepository = ServiceLocator.Current.GetInstance<IVisitorGroupRepository>();
                var visitorGroupRoleRepository = ServiceLocator.Current.GetInstance<IVisitorGroupRoleRepository>();

                var helper = new VisitorGroupHelper(visitorGroupRoleRepository);
                var groups = visitorGroupRepository.List();
                
                foreach (var visitorGroup in groups)
                {
                    if(helper.IsPrincipalInGroup(User,visitorGroup.Name))
                        // add to your collection
                }
#188276
Feb 16, 2018 14:08
Vote:
 

Hello! Thanks for your answer!

For me it worked the code below, in case there is someone who needs this.

private List<string> GetVisitorGroupIdsByCurrentUser(HttpContextBase httpContext)
{
var visitorGroupId = new List<string>();
var user = httpContext.User;
var visitorGroups = _visitorGroupRepository.List();
foreach (var visitorGroup in visitorGroups)
{
if (_visitorGroupRoleRepository.TryGetRole(visitorGroup.Name, out var virtualRoleObject))
{
if (virtualRoleObject.IsMatch(user, httpContext))
{
visitorGroupId.Add(visitorGroup.Id.ToString());
}
}
}

return visitorGroupId;
}



#188292
Edited, Feb 19, 2018 9:10
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.