Epi User Picker Property that uses the built-in Membership Browser

Maybe I have poor Googling skills but I couldn't find a simple user picker property anywhere. We had some examples at work but they involved a custom user control and too much JavaScript.

Looked around a bit in the page EditSecurity Web Form and found the JavaScript function OpenDialogUserAndGroupBrowser in ui/cms/javascript/system.js. It was easy to see how this worked and I just needed to pass the textbox as a javascript object as the callback parameter.

namespace Krompaco.Project.PropertyTypes
    using System;
    using System.Web.UI.HtmlControls;
    using EPiServer;
    using EPiServer.PlugIn;

    [PageDefinitionTypePlugIn(DisplayName = "PropertyUserPicker")]
    public class PropertyUserPicker : EPiServer.Core.PropertyString
        public override EPiServer.Core.IPropertyControl CreatePropertyControl()
            return new PropertyUserPickerControl();

        private class PropertyUserPickerControl :<br>
            public override void CreateEditControls()
                string callbackScript = @"<script type=""text/javascript"">
                    function KrompacoBrowserDialogCompleted(returnValue, textbox) {
                        if (!returnValue) {

                        if (returnValue.length > 0) {
                            var lastAddedRow = returnValue[returnValue.length - 1].split(';');

                            if (lastAddedRow[1] == 1) {
                                alert('You selected a group. Select a user instead.');

                            textbox.value = lastAddedRow[0];

                if (!Page.ClientScript.IsStartupScriptRegistered(this.GetType(),

                HtmlInputButton btn = new HtmlInputButton();
                btn.Value = "...";
                btn.Attributes.Add("class", "epismallbutton");
                btn.Attributes.Add("onclick", "OpenDialogUserAndGroupBrowser(0, '',
                    '" + UriSupport.AbsoluteUrlFromUIBySettings("edit") + "', true,
                    document.getElementById('" + this.EditControl.ClientID + "'), null);");

As you can see it's easy to modify this control to for instance allow multiple items or one group name. If you decide to go multiple items you should probably inherit from the long string property instead.

Code is tested in Episerver CMS 6 R1 and 6 R2.

Published and tagged with these categories: Episerver, ASP.NET, Development