Wednesday, October 17, 2007

Using the SharePoint People Picker


A long time ago (beta2 time) I published a post about the people picker control (MOSS 2007 controls - have a bit of fun with the "people editor" form control!).
Lately I had many comments on that post from people who have been having issues getting the value the user selected, and asked for another demo of using the control.
So here it is:



The code creates three controls - a PeopleEditor (from the Microsoft.SharePoint.WebControls namespace), a button and a textbox. The button has an click event that itirates over the users picked in the PeopleEditor and writes them to the text box. Simple!





public class PeoplePickerWebPart : System.Web.UI.WebControls.WebParts.WebPart

    {

        PeopleEditor pe;

        TextBox t;

        Button b;

 

        protected override void CreateChildControls()

        {

            base.CreateChildControls();

            pe = new PeopleEditor();

 

 

            this.Controls.Add(pe);

            b = new Button();

            b.Text = "Click me to see the users";

            this.Controls.Add(b);

            b.Click += new EventHandler(b_Click);

            t = new TextBox();

            t.TextMode = TextBoxMode.MultiLine;

            this.Controls.Add(t);

        }

 

        void b_Click(object sender, EventArgs e)

        {

            foreach(string ent in pe.CommaSeparatedAccounts.Split(','))

            {

                t.Text += ent + Environment.NewLine;

            }

        }








Results:

The web part as the page loads





The web part does "check names"





The web part after the click of the button







1 comment:

Rodney said...

This post came at a great time. I'm trying to build a dataview webpart in SP Designer that is linking to the sites list in the site directory, but when it displays the "Owner" field, it shows every possible listing for the username, instead of the default, "Last, First". Any ideas on how to manipulate the data with the xpath designer tool to show that? Great post :)