Mahdi Shahbazi
Jan 4, 2017
(1 votes)

How to Check Visitor Group Criteria In Code

If you want to check if a contentAreaItem is match current user all visitor group criteria you can use the following Methods:

  public static bool IsMatchCriteria(ContentAreaItem contentAreaItem)
            var result = true;
            contentAreaItem.AllowedRoles.ForEach(role =>
                    result = result && IsMatchCriteria(role);
            return result;

        public static bool IsMatchCriteria(string guid)
            var visitorGroupGuidId = new Guid(guid);
            var visitorGroupRepository = ServiceLocator.Current.GetInstance<IVisitorGroupRepository>();
            var user = HttpContext.Current.User;

            var vgHelper = new VisitorGroupHelper();
            var visitorGroup = visitorGroupRepository.Load(visitorGroupGuidId);
            return vgHelper.IsPrincipalInGroup(user, visitorGroup.Name);

[Pasting files is not allowed][Pasting files is not allowed][Pasting files is not allowed][Pasting files is not allowed]

Jan 04, 2017


Aria Zanganeh
Aria Zanganeh Jan 4, 2017 08:04 PM

Great job mate!

valdis Jan 4, 2017 09:42 PM

just my 2 cent here:

a) why not convert "role" from IsMatchCriteria method to Guid already and pass in that, instead of convert string to Guid?

b) I would try to avoid using ServiceLocator in this context, but instead - would require from "above" - somebody has to inject that dependency in order to test for visitor group match.

Please login to comment.
Latest blogs
Plug-in manager is back in CMS 12

Plug-in manager is back in the UI, what is it and how can i use it?

Luc Gosso (MVP) | Oct 6, 2022 | Syndicated blog

Display Child Pages in Content Delivery API Response

The below example will implement an instance of IContentConverterProvider to customise the serialisation of PageData and output child pages in the...

Minesh Shah (Netcel) | Oct 4, 2022

Bring the Report Center back in Optimizely CMS 12

The Report Center has been a part of Optimizely CMS since its first debut in version 5R2 in 2008, but in CMS 12, it's removed! Don't despair! Make...

Tomas Hensrud Gulla | Oct 4, 2022 | Syndicated blog

Customizing Property Lists in Optimizely CMS

Generic property lists is a cool editorial feature that has gained a lot of popularity - in spite of still being unsupported (officially). But if y...

Allan Thraen | Oct 2, 2022 | Syndicated blog