Views: 3976
Number of votes: 1
Average rating:

Custom property for selecting Visitor Groups

This custom property will render a checkbox list of all visitor groups, allowing multiple selections.

image 

Add a reference to EPiServer.ApplicationModules.

using EPiServer.Personalization.VisitorGroups;
 
namespace MySite.Website.Plugins.SpecializedProperties.VisitorGroups
{
    [PageDefinitionTypePlugIn(DisplayName = "Visitor Group selection")]
    public class PropertyVisitorGroups : PropertyMultipleValue
    {
        public override IPropertyControl CreatePropertyControl()
        {
            return new PropertyVisitorGroupsControl();
        }
    }
 
    public class PropertyVisitorGroupsControl : PropertySelectMultipleControlBase
    {
        protected override void SetupEditControls()
        {
            var repository = new VisitorGroupStore(DynamicDataStoreFactory.Instance);
            var list = repository.List()
                .OrderBy(v => v.Name)
                .Select(v => new ListItem(v.Name, v.Id.ToString())
                        {
                            Selected = ((PropertyMultipleValue) PropertyData).IsValueActive(v.Id.ToString())
                        })
                .ToArray();
 
            EditControl.Items.AddRange(list);
        }
    }
}
Apr 20, 2011

Please login to comment.