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 :
EPiServer.Web.PropertyControls.PropertyTextBoxControlBase { public override void CreateEditControls() { base.CreateEditControls(); string callbackScript = @"<script type=""text/javascript""> function KrompacoBrowserDialogCompleted(returnValue, textbox) { if (!returnValue) { return; } if (returnValue.length > 0) { var lastAddedRow = returnValue[returnValue.length - 1].split(';'); if (lastAddedRow[1] == 1) { alert('You selected a group. Select a user instead.'); return; } textbox.value = lastAddedRow[0]; } } </script>"; if (!Page.ClientScript.IsStartupScriptRegistered(this.GetType(), "PropertyUserPickerDialog")) { Page.ClientScript.RegisterStartupScript(this.GetType(), "PropertyUserPickerDialog", callbackScript); } HtmlInputButton btn = new HtmlInputButton(); btn.Value = "..."; btn.Attributes.Add("class", "epismallbutton"); btn.Attributes.Add("onclick", "OpenDialogUserAndGroupBrowser(0, '', '" + UriSupport.AbsoluteUrlFromUIBySettings("edit") + "', true, KrompacoBrowserDialogCompleted, document.getElementById('" + this.EditControl.ClientID + "'), null);"); Controls.Add(btn); } } } }

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.

Added Friday 5 November 2010 11:28 — Tags for this page: Episerver, Development


Add Your Comment