Wednesday, July 29, 2009

Reading values from People picker control

Scenario:
Reading values from People picker control

Solution:
Here's I am posted sample code, Sorry for hardcoding the item id and other values.Please make changes according to requirement.

Single User/Group selection:


using(SPSite site = new SPSite("http://localhost"))
{
using(SPWeb web = site.OpenWeb())
{
SPList list = web.Lists["Tasks"];
SPListItem item = list.GetItemById(1);

SPFieldUserValue userValue = new SPFieldUserValue(web, item["AssignedTo"].ToString());
Console.WriteLine(userValue.User.Email);

}
}
Multi User/Group selection:
using(SPSite site = new SPSite("http://localhost"))
{
using(SPWeb web = site.OpenWeb())
{
SPList list = web.Lists["Tasks"];
SPListItem item = list.GetItemById(1);

SPFieldUserValueCollection userValue = new SPFieldUserValueCollection(web, item["AssignedTo"].ToString());
Console.WriteLine(userValue[0].User.Email);
}
}

1 comments:

PaLa May 29, 2012 at 8:47 AM  

Helped me! thanks a lot...