Implementation of People Picker in Web parts

1. Introduction

There can be applications where we may need to add people or group to a share point list. The people picker control is available inbuilt when we operate through out of box for adding/editing people or group in share point lists. But, when the requirement is to include a people picker in web part, then there are certain properties and methods to be defined.
In this paper, the different sections discussed are:
· Declaring and assigning properties to a people picker control.
· Obtaining the properties of value added in the control (like display name, email ID)
· Clearing the values in a control.
· Updating the list item with the values in the people picker control.
· Populating the values to a people picker control from the list.
· add multiple user into “person and group” column of a list programmatically.


2. Declaration and assigning properties to People Picker.

The first step for including a people picker in a web part would be to declare an object for this control. This section discusses about the declaration and assigning properties to a people picker control. The people picker is enabled through a class called PeopleEditor. The declaration of PeopleEditor is as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//Class definitions to be included for enhancing People Picker
using System. Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint.Portal.UserProfiles;
using Microsoft.SharePoint.Portal.Topology;
using Microsoft.SharePoint.Portal;
//Declaring PeopleEditor object
PeopleEditor ctrlpplPicker = new PeopleEditor ();
//Defining properties of the PeopleEditor
ctrlpplPicker.AutoPostBack = false;
ctrlpplPicker.PlaceButtonsUnderEntityEditor = true;
ctrlpplPicker.ID = "pplEditor";
ctrlpplPicker.AllowEmpty = false;
ctrlpplPicker.SelectionSet = "User, DL, SecGroup, SPGroup";
ctrlpplPicker.MultiSelect = true;
ctrlpplPicker.MaximumEntities = 5;

The AutoPostBack property makes a page refresh on addition of value. It is assigned to false in order to avoid page refreshing.
The PlaceButtonsUnderEntityEditor property decides the location of the Names and Browse buttons of the people picker.
The AllowEmpty property lets you define if the user is required to fill in a value in the people picker.
The SelectionSet property allows you to define the set of users and groups the people picker can choose from.
The Maximum Entities property restricts the maximum number of people or groups to be added to the control.

you may think that How do you create a multi select people picker field programatically in SharePoint 2007? or in sharepoint 2010?
Answer is very simple. Set MultiSelect property to true.

3. Obtaining details of the entries added in the people picker

Once the people picker control is added in a webpart, the next task would be to resolve the entries made, obtain the details of the entries (like display name, email ID) and update those details to the list. The below function explains about obtaining details of the entries made in the people picker.

1
2
3
4
5
// pplInfo is a string array which is used to hold display name and the email ID of the entries.
//Declaring pplInfo
String[] pplInfo = new String[2];
pplInfo[0] = String.Empty;
pplInfo[1] = String.Empty;

objEntity is an object of Picker Entity class which stores the individual entry (people or group) added in the people picker control. This object is required to obtain the details of the people or group added in the control. objPplEditor.ResolvedEntities is a property of Picker Entity which resolves the entries made in the people picker control.
The below for loop gets the count of number of entries made in the people picker. For every entry made, pplInfo [0] stores DisplayName and pplInfo [1] stores email ID.

1
2
3
4
5
6
7
8
9
10
11
for(int i = 0; i < objPplEditor.ResolvedEntities.Count; i++)
{
	PickerEntity objEntity = (PickerEntity) objPplEditor.ResolvedEntities[i];
	pplInfo [0] += objEntity.EntityData["DisplayName"] + ";";
	pplInfo [1] += objEntity.EntityData["Email"] + ";";
}
if (pplInfo[0] != String.Empty)
{
	pplInfo[0] = pplInfo[0].Substring(0, pplInfo[0].Length - 1);
	pplInfo[1] = pplInfo[1].Substring(0, pplInfo[1].Length - 1);
}

From the above coding snippet, the string array pplInfo holds the DisplayName and email ID of the entries made.

4. Updating people picker control values to a list item

Now we have the display name and the email ID of the entries made in people picker control in a string array pplInfo (pplInfo [0] stores Display Name and pplInfo [1] stores email ID). These values can be used to store display name and email ID in the list item.

1
2
3
4
5
6
7
8
9
10
11
//Open the share point site which contains the list in which the entities of the people picker is to be updated.
SPWeb myweb = new SPSite(siteName).OpenWeb();
//Get the list name from the share point site. mylist is a SPList object which points to the sharepoint list in the site.
SPList mylist = myweb.Lists[listName];
// ‘newitem’ is a SPListItem object which points to new list item to be added to the list.
SPListItem newitem = mylist.Items.Add();
//The display name and Email ID of the values entered in people picker control are updated to the list as shown below.
//Update () is the function which is used to update the list item.
newitem[columnName_DisplayName] = pplInfo[0];//Display name
newitem[columnName_EmailID] = pplInfo[1];//Email ID
newitem.Update( );

5. Clearing existing entities in the control

Once the control values are updated to the list we need to clear the control. The people editor control has property Entities.Clear( ) which clears the entries made in people picker.

1
ctrlpplPicker.Entities.Clear();

6. Populating values to control from the list

There can be a requirement where we need obtain the details of a list item from the sharepoint list and populate the obtained values to corresponding controls for viewing or editing. For example, in the case of meeting details, the attendees will be populated to people picker control, meeting name to textbox control, etc. The below section explains how the values are obtained from the list and populated to the control.
The string variable pplNames_List holds the display names on getting the values from the list. Every display name is separated by semi-colon (;). For example, the display names obtained from the list will be of the form Sachin (WT-01 TIS); Edwin (WT-01 Retail).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
string pplNames_List= "";
pplNames_List= master[columnName_DisplayName].ToString();
// pplNames holds the display name on resolving entities pplNames_List separated by semi-colon
string[] pplNames;
pplNames = pplNames_List.Split(';');
//Array list holds the complete list of names to be added to the control.
//The People Editor has a property UpdateEntities which populates the control with the values in Array List.
System.Collections.ArrayList pplList = new System.Collections.ArrayList();
// For every value present in the pplNames, the PickerEntity object ‘entity’ holds the value and entity is added to pplList.
for (int pplCounter = 0; pplCounter < pplNames.Length; pplCounter++)
{
	PickerEntity entity = new PickerEntity();
	entity.Key = pplNames[pplCounter];
	pplList.Add(entity);
}
//At the end of this for loop, the Array list pplList will hold all the values retrieved from the list item for populating the control.
//The PeopleEditor has a property UpdateEntities( ArrayList ) which populates the people picker control with the values stored in the array list.
ctrlpplPicker.UpdateEntities(pplList);

7. References

MSDN site which contains PeopleEditor class MSDN

Thanks!
Avinash

calendarMarch 14, 2012 · cardInfoyen · comments6 Comments
tagTags: , , , , ,  · Posted in: MOSS, SharePoint

6 Responses

  1. Reenu - April 10, 2012

    Hi Avinash,

    I have got a custom list called a user tasks…
    Where I am supposed to pick users who are going to work for each tasks. I believe that i can give a peoplepicker in a webpart to select the users.

    However the real problem I am facing is, Each task in the list, The users working on it will be different. How can I add Multiple users to the list field called “Team” in that list from People picker.

    Thanks….

  2. Infoyen - April 11, 2012

    Hi,

    Your question is same like below question:-
    How to add a user and multi users to “person and group” column of a list? or
    How to add a user and multi users to the People picker field in a list programtically

    I have created a sharepoint list named as “user tasks” and created a column name as “Team” type of “Person or Group” which is configured as “Allow multiple selections: Yes”
    I have created a user control which has PeopleEditor and submit button. see below code:-

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    
            protected override void CreateChildControls()
            {
                try
     
                {
                    base.CreateChildControls();
                    btnSubmit = new Button();
                    btnSubmit.Text = "Submit";
                    btnSubmit.ID = "_btnSubmit";
                    btnSubmit.Click += new EventHandler(btnSubmit_Click);
     
                    //Declaring PeopleEditor object
                    ctrlpplPicker = new PeopleEditor();
                    //Defining properties of the PeopleEditor
                    ctrlpplPicker.AutoPostBack = false;
     
                    //ctrlpplPicker.PlaceButtonsUnderEntityEditor = true;
                    ctrlpplPicker.ID = "pplEditor";
                    ctrlpplPicker.AllowEmpty = false;
                    //ctrlpplPicker.SelectionSet = "User, DL, SecGroup, SPGroup";
                    ctrlpplPicker.MultiSelect = true;
                    ctrlpplPicker.MaximumEntities = 5;
                    this.Controls.Add(ctrlpplPicker);
                    this.Controls.Add(btnSubmit);
                }
                catch
                {
                    HandleException(ErrorMessage);
                }
            }

    Now i wrote below code which add users in “User tasks” list programetically on click of submit button:-

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    
           void btnSubmit_Click(object sender, EventArgs e)
            {
                //ctrlpplPicker is object of PeopleEditor
                string[] userarray = ctrlpplPicker.CommaSeparatedAccounts.ToString().Split(',');;
                SPFieldUserValueCollection usercollection = new SPFieldUserValueCollection();
     
                for (int i = 0; i < userarray.Length; i++)
                {
                    SPFieldUserValue usertoadd = ConvertLoginAccount(userarray[i]);
                    usercollection.Add(usertoadd);
                }
     
                SPList list = SPContext.Current.Web.Lists["user tasks"];
                SPListItem item = list.Items.Add();
                item["Team"] = usercollection;            
                item.Update();
     
            }
            public SPFieldUserValue ConvertLoginAccount(string userid)
            {
                SPFieldUserValue uservalue;
                using (SPSite site = new SPSite(SPContext.Current.Site.Url))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPUser requireduser = web.EnsureUser(userid);
                        uservalue = new SPFieldUserValue(web, requireduser.ID, requireduser.LoginName);
                    }
                }
                return uservalue;
            }

    Hope this helps.

    Regards,
    Avinash

  3. shaver - April 9, 2013

    Great post.

  4. Mihir - April 17, 2013

    Super solution. Bookmarked this page.

  5. Siva - September 11, 2013

    What are the maximum people we can add in the people picker control at a time? and what is the maximum limit we can set MaximumEntities property??

  6. Infoyen - September 25, 2013

    I dont have perfect answer to your question. So please share if you get answer to your question.

    Also go to below url which explain about “SharePoint Server 2010 capacity management”
    http://technet.microsoft.com/en-us/library/cc262787(v=office.14).aspx

    Regards,
    Infoyen

Leave a Reply

Spam Protection: , required

myworldmaps infoyen