Get User field value

13/07/2017 13:34
 public static SPUser GetUserFromField(SPListItem item, string fieldName)
{
SPUser user = null;
if (item != null && !string.IsNullOrEmpty(fieldName))
{
if (TryGetFieldFromItem(item, fieldName) != null && item[fieldName] != null)
{
var userFieldValue = new SPFieldUserValue(item.Web, item[fieldName].ToString());
if (userFieldValue.User != null) //can happen in case of group
user = userFieldValue.User;
}
}
return user;
}
 
 
public static List<SPUser> GetUsersFromField(SPListItem item, string fieldName)
{
var users = new List<SPUser>();
if (item != null && !string.IsNullOrEmpty(fieldName))
{
if (TryGetFieldFromItem(item, fieldName) != null && item[fieldName] != null)
{
var userFieldValues = new SPFieldUserValueCollection(item.Web, item[fieldName].ToString());
 
if (userFieldValues.Count > 0)
{
foreach (var userVal in userFieldValues)
{
if (userVal.User != null)
users.Add(userVal.User);
}
}
}
}
return users;
}