Pages

Showing posts with label People/Group Sharepoint Field. Show all posts
Showing posts with label People/Group Sharepoint Field. Show all posts

Wednesday, June 17, 2009

How to display users to SharePoint People Picker control and get data from SharePoint People Picker control

In below example, I’ll demonstrate how to display users to people picker web control and get data from the same control. I have field called “Staff Assigned” and its data type is People/Group.

Below code will tell us how we can take data from that people/Group field and display to people picker SharePoint web control and also get data from people picker and give back to people/Group field from object model programmatically.

//Display users from people/Group field to People Picker Control

if (itmAudit["Staff Assigned"] != null)
{
char[] to_splitter = { ';' };
string to_list = itmAudit["Staff Assigned"].ToString(); // Reads value stored in SPList. (i.e., "Domain\User1; Domain\User2")
string[] arr = to_list.Split(to_splitter);
string user = string.Empty;
System.Collections.ArrayList entityArrayList = new System.Collections.ArrayList();
for (int i = 1; i < arr.Length; i++)
{
if ((i % 2) != 0)
{
user = arr[i].Substring(arr[i].IndexOf("#") + 1);
PickerEntity entity = new PickerEntity();
entity.Key = user;
entity = userPicker.ValidateEntity(entity);
entityArrayList.Add(entity);
}
}

userPicker.UpdateEntities(entityArrayList);

//Save users to people/Group field from People Picker Control

SPFieldUserValueCollection values = new SPFieldUserValueCollection();
foreach (PickerEntity entity in userPicker.ResolvedEntities)
{
SPFieldUserValue fuv = new SPFieldUserValue(SPContext.Current.Web, Convert.ToInt16(entity.EntityData[PeopleEditorEntityDataKeys.UserId]), entity.Description);

values.Add(fuv);
}

itmAudit["Staff Assigned"] = values;