Using the People Picker in Visual Studio

In Visual Studio, you can use the People Picker if you are developing for the SharePoint platform. It was actually quite easy:

– go to your tool pane and right click "Choose Items…"

– scroll to the "PeopleEditor" and add this to your tool pane

– switch you view to design and drag this control on your page. Don’t worry the error message, if you build your solution, it will succeed

– now, you can set properties design time on this control, or do this via code. The textbox with the images will show up automatically, usage is as normal in a standard site

– by iterating through the resolved entities you can roll up each user in the people picker. Easy!

I also want to apply filtering (for example: only Groups and users which start with ABC). You can do this on site collection level with STSAdm: "Stsadm.exe -o setproperty –url http://sharepoint –pn peoplepicker-onlysearchwithinsitecollection –pv yes" .However, I would like to do this in code, so I do not interfere with other team members’ work. So, I will have to do some coding…

Here is the code from a user control which will show the people picker. This user control is rendered in the SmartPart web part (must have!). Note here that you cannot test this in your development environment; you will have to copy the user control to your Smart Part directory and fire up your SharePoint site.

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

//added:
using System.Runtime.InteropServices;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
public partial class usrPeoplePicker : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //Alternative: set controls on object itself
        PeopleEditor1.AutoPostBack = true;
        PeopleEditor1.PlaceButtonsUnderEntityEditor = true;
        PeopleEditor1.ID = "pplEditor";
        PeopleEditor1.AllowEmpty = false;
        PeopleEditor1.SelectionSet = "User,DL,SecGroup,SPGroup";
        PeopleEditor1.MultiSelect = true;
    }
    private string GetAccountName()
    {
        //read contents of people picker control
        string _ret = "";
        foreach (PickerEntity ent in PeopleEditor1.ResolvedEntities)
        {
            _ret += ";" + ent.Key;
        }
        return _ret;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        lblAccount.Text = GetAccountName();
    }
}

Leave a comment