Wednesday, April 22, 2009

Get a user from a list item

Very often we need to obtain username or group name from the list which has column type set to "person and group". The below code snippet might be handy



using (SPSite site = new SPSite(http://mysite))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists["myList"];
SPListItem item = list.Items[0];

SPFieldUserValue userValue = new SPFieldUserValue(web, item["User Name"].ToString());
if (userValue.User.LoginName == web.CurrentUser.LoginName)
{
//current user found in the list
}
}
}

No comments:

Post a Comment