Tuesday, April 22, 2008

Implementation of People Picker Control in Custom Web Parts

1. Declaration and assigning properties to People Picker.
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 MaximumEntities property restricts the maximum number of people or groups to be added to the control.


2. 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 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 < objentity =" (PickerEntity)">


3. 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();
//Gets the list name from the share point 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( );



4. 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();



5. Populating values to control from the list
There can be a requirement where we need to 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.
//Holds the display names on getting the values from the list. Every display name is separated by semi-colon(;)
string pplNames_List= "";
pplNames_List= master[columnName_DisplayName].ToString();
//Holds the display name on resolving entities pplNames_List separated by semi-colon
string[] pplNames;
pplNames = pplNames_List.Split(';');
//Arraylist which holds the complete list of names to be added to the control
System.Collections.ArrayList pplList = new System.Collections.ArrayList();
//Store all the values in pplNames in PickEntity object entity and store the entities to pplList.
for (int pplCounter = 0; pplCounter < entity =" new" key =" pplNames[pplCounter];">