Multiple checkbox values in custom visitor group criterion

Vote:
 

This question is similar to https://world.episerver.com/forum/developer-forum/-Episerver-75-CMS/Thread-Container/2021/2/multiple-string-values-in-custom-visitor-group-criterion/ (and funny it was posted just before I posted this), but in my case I'm creating a Criterion for selecting Episerver User Groups / Roles and I have two problems:

  1. What property type should the model have ("Groups" below)?
  2. How do I display a list of labeled checkboxes in the UI, i.e is there a DojoWidget that works? I am currently using MultiSelect and it displays the items fine, but I would prefer something like the weekday editor in the TimeOfDay criterion, i.e similar to the SelectMany attribute

With the setup below, I get a "Failed to convert item" error when I try to save it in episerver:

	public class UserGroupsCriterionModel : CriterionModelBase
	{
		[DojoWidget(WidgetType = "dijit.form.MultiSelect", SelectionFactoryType = typeof(UserGroupSelectionFactory))]
		public string[] Groups { get; set; }

		public override ICriterionModel Copy()
		{
			return base.ShallowCopy();
		}
	}

	public class UserGroupSelectionFactory : EPiServer.Personalization.VisitorGroups.ISelectionFactory
	{
		public IEnumerable<SelectListItem> GetSelectListItems(Type propertyType)
		{
			return System.Web.Security.Roles.GetAllRoles().Select(t => new SelectListItem { Text = t, Value = t });
		}
	}

	[VisitorGroupCriterion(Category = "Custom", Description = "", DisplayName = "User Groups")]
	public class UserGroupsCriterion : CriterionBase<UserGroupsCriterionModel>
	{
		public override bool IsMatch(IPrincipal principal, HttpContextBase httpContext)
		{
			return httpContext.User.Identity.IsAuthenticated && Model.Groups.Intersect(System.Web.Security.Roles.GetRolesForUser(httpContext.User.Identity.Name)).Any();
		}
	}
#249150
Feb 24, 2021 13:56
Vote:
 

There's already a role criterion in the https://world.episerver.com/add-ons/visitor-group-criteria-pack/ that can be used. You can define multiple by just adding it multiple times in the Visitor Group and setting the match to Any

#249152
Feb 24, 2021 14:54
Peter Törnqvist - Feb 24, 2021 14:57
Ah, great! Didn't know that. I'll check it out. Thanks!
Vote:
 

However if you want to build your own I'd follow https://world.episerver.com/documentation/developer-guides/CMS/personalization/developing-custom-visitor-group-criteria/ and decompile the code for the TimeOfDayCriterion and how it's built as it has a multi picker checkbox control.

#249153
Feb 24, 2021 14:56
Peter Törnqvist - Feb 24, 2021 14:59
Sadly, the TimeOfDayCriterion uses 7 boolean properties for the weekdays and a custom DropDownButton widget, so it doesn't really help me :/
Scott Reed - Feb 24, 2021 15:06
Ah I didn't realize it was that basic I just knew it had multi checkbox. I would imagine you can still adapt the script and how the item is saved however? But hopefully the other suggesting criteria pack will do your job
Peter Törnqvist - Feb 24, 2021 16:33
Yes, I was hoping to avoid creating a custom diji widget myself, but maybe I will end up there eventually...
Vote:
 

I could live with the dijit MultiSelect, but what would be the correct property type for the Groups property in that case? I just can't figure it out....

#249166
Feb 24, 2021 18:16
Vote:
 

I've found that the only property type that works i object[], don't ask me why string[] in't good enough (string[] is what dijit sends back to the backend): 

public class UserGroupsCriterionModel : CriterionModelBase
	{
		[DojoWidget(WidgetType = "dijit.form.MultiSelect", SelectionFactoryType = typeof(UserGroupSelectionFactory))]
		public object[] Groups { get; set; }

		public override ICriterionModel Copy()
		{
			return base.ShallowCopy();
		}
	}

One other thing to remember is that criterions work differently from normal Episerver page and block classes - if you've added a property to a class and used it, you can't change the property type! I've had to actually rename the model class several times to get it to work when I changed the type. Now I only wish there was a multiselect checkbox list dijit widget I could use :)

#249200
Edited, Feb 25, 2021 5:12
Tim Schmelter - May 08, 2021 9:06
Thank you.
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.