Get users from item user field
01/12/2017 12:06
public static List<SPUser> GetUsersFromField(SPListItem item, string fieldName)
{
SPGroup group = null;
var users = new List<SPUser>();
if (item.Fields.ContainsField(fieldName) == false)
return users;
if (item[fieldName] == null)
return users;
var userValueString = item[fieldName].ToString();
if (string.IsNullOrEmpty(userValueString))
return users;
var field = item.Fields.TryGetFieldByStaticName(fieldName) as SPFieldUser;
if (field.AllowMultipleValues)
{//multiple values
var usersValueCollection = item[fieldName] as SPFieldUserValueCollection;
if (usersValueCollection == null)
return users;
foreach (var userValue in usersValueCollection)
{
var userFieldValue = new SPFieldUserValue(item.Web, userValue.ToString());
if (userFieldValue.User == null)
{//group
group = item.Web.SiteGroups[userFieldValue.LookupValue];
var groupUsers = GetUsersFromGroup(group);
users.AddRange(groupUsers);
}
else
{//user
var user = item.Web.EnsureUser(userFieldValue.User.LoginName);
users.Add(user);
}
}
}
else //single value
{
var userFieldValue = new SPFieldUserValue(item.Web, userValueString);
if (userFieldValue.User == null)
{//group
group = item.Web.SiteGroups[userFieldValue.LookupValue];
var groupUsers = GetUsersFromGroup(group);
users.AddRange(groupUsers);
}
else
{//user
var user = item.Web.EnsureUser(userFieldValue.User.LoginName);
users.Add(user);
}
}
var uniqueUsers = new List<SPUser>();
var userIds = users.Select(u => u.ID).Distinct().ToList();
foreach (var id in userIds)
{
var user = users.Where(u => u.ID == id).FirstOrDefault();
uniqueUsers.Add(user);
}
return uniqueUsers;
}
public static List<SPUser> GetUsersFromGroup(SPGroup group)
{
var users = new List<SPUser>();
if (group != null)
{
foreach (SPUser userItem in group.Users)
{
if (!users.Select(u => u.ID).Contains(userItem.ID))
{
users.Add(userItem);
}
}
}
return users;
} public static List<SPUser> GetUsersFromField(SPListItem item, string fieldName)
{
SPGroup group = null;
var users = new List<SPUser>();
if (item.Fields.ContainsField(fieldName) == false)
return users;
if (item[fieldName] == null)
return users;
var userValueString = item[fieldName].ToString();
if (string.IsNullOrEmpty(userValueString))
return users;
var field = item.Fields.TryGetFieldByStaticName(fieldName) as SPFieldUser;
if (field.AllowMultipleValues)
{//multiple values
var usersValueCollection = item[fieldName] as SPFieldUserValueCollection;
if (usersValueCollection == null)
return users;
foreach (var userValue in usersValueCollection)
{
var userFieldValue = new SPFieldUserValue(item.Web, userValue.ToString());
if (userFieldValue.User == null)
{//group
group = item.Web.SiteGroups[userFieldValue.LookupValue];
var groupUsers = GetUsersFromGroup(group);
users.AddRange(groupUsers);
}
else
{//user
var user = item.Web.EnsureUser(userFieldValue.User.LoginName);
users.Add(user);
}
}
}
else //single value
{
var userFieldValue = new SPFieldUserValue(item.Web, userValueString);
if (userFieldValue.User == null)
{//group
group = item.Web.SiteGroups[userFieldValue.LookupValue];
var groupUsers = GetUsersFromGroup(group);
users.AddRange(groupUsers);
}
else
{//user
var user = item.Web.EnsureUser(userFieldValue.User.LoginName);
users.Add(user);
}
}
var uniqueUsers = new List<SPUser>();
var userIds = users.Select(u => u.ID).Distinct().ToList();
foreach (var id in userIds)
{
var user = users.Where(u => u.ID == id).FirstOrDefault();
uniqueUsers.Add(user);
}
return uniqueUsers;
}
public static List<SPUser> GetUsersFromGroup(SPGroup group)
{
var users = new List<SPUser>();
if (group != null)
{
foreach (SPUser userItem in group.Users)
{
if (!users.Select(u => u.ID).Contains(userItem.ID))
{
users.Add(userItem);
}
}
}
return users;
}