Try our conversational search powered by Generative AI!

Display content to end users depending on group

Vote:
 

Hi,

What is the best way to display text or content depending on what group an end user is part of? 

When a logged in user visits a page, I want to display some text in case they are part of one group, and some other text if they belong to the other group.

I have tried to look at examples about visitor groups, but they usually base the displaying of content on things like location or time of day while I want to go to the backend and check what group the logged in user is part of, and based on that display the right content.

Are visitor groups the way to go or is there some other solution I can read up on?

Thanks!

#226134
Aug 05, 2020 9:53
Vote:
 

You could create your own critera, where you check what group the user is part of: 

https://world.episerver.com/documentation/developer-guides/CMS/personalization/example-create-your-own-visitor-group-criteria/

Just implement the IsMatch-method to suit your needs.

#226139
Aug 05, 2020 10:17
joakim - Aug 05, 2020 14:44
Thanks a lot for the advice. I will give this approach a go.
Vote:
 

Hi Joakim,

The visitor group is criteria the best approach to show/hide content on basis of user group and if your criteria do not match in builtin visitor group then visit the below URL and create own visitor group criteria and applied above the content https://world.episerver.com/documentation/developer-guides/CMS/personalization/example-create-your-own-visitor-group-criteria/

And if you want to display specific content for the only logged-in users then use HttpContext.Current.User and check the user is anonymous or logged In and show/hide content.

Thanks!

#226140
Aug 05, 2020 10:17
joakim - Aug 05, 2020 14:45
Thanks! This is what I was looking for.
Vote:
 

If you want to display plain text that differ based on group, you could also do something like this. Create a helper class.

using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using EPiServer.Security;

namespace Alloy.Helpers
{
    public static class WelcomeUserHelper
    {
        private static readonly Dictionary<string, string> WelcomeMessages = new Dictionary<string, string>()
        {
            { "WebAdmins", "Hey admin!" },
            { "WebEditors", "Hey editor!" }
        };

        public static string Welcome(this HtmlHelper helper)
        {
            foreach (var (group, message) in WelcomeMessages.Select(x => (x.Key, x.Value)))
            {
                if (PrincipalInfo.Current.RoleList.Contains(group))
                {
                    return message;
                }
            }

            return string.Empty;
        }
    }
}

Add your messages to the top dictionary, and include in your view like this:

@Html.Welcome()

Does that work for you?

If the user is a member of multiple groups included in your configuration, only the first will be shown. Order your groups accordingly, or modify the code to display multiple messages in those cases.

#226142
Edited, Aug 05, 2020 10:39
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.