Monday, March 15, 2010

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. For example, there can be a list which stores the meeting details. The meeting details include meeting name, meeting date and time, attendees, meeting facilitator, etc. In this case the attendees and meeting facilitator are of the type people or group.
People Picker is control through which we can add people or group in a share point application. 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.

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:
//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. If you set it to true, the buttons are placed at the bottom right corner of the account name text box, otherwise to the right of it.
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. This set can consist of users, Active Directory distribution lists, Active Directory security groups, and SharePoint groups.
The MultiSelect property lets you define if a user is allowed to specify multiple user accounts.
The Maximum Entities property restricts the maximum number of people or groups to be added to the control.

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.
// 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.
for(int i = 0; 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.
//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.
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).
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 <>
{
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

A. MSDN site which contains PeopleEditor class
http://msdn2.microsoft.com/en-us/library/ms468579.aspx
B. Explanation for PeopleEditor properties and methods. Function for resolving entities.
http://www.lcbridge.nl/vision/2007/peoplepicker.htm

Author:- Avinash Kumar Dad
Location: Hyderabad, India

No comments: