private object GetUserValue(SPListItem item, object value) { object valueResult = null; var personInfo = (User)value; if (personInfo.ID != 0) { valueResult = new SPFieldUserValue(item.Web, personInfo.ID, personInfo.UserName); } else if (personInfo.IsGroup) { SPGroup group = item.Web.Groups.GetByID(personInfo.ID); if (@group != null) { valueResult = new SPFieldUserValue(item.Web, @group.ID, @group.LoginName); } } else { SPUser userOrGroupToAdd = string.IsNullOrEmpty(SiteUrl) ? SPHelper.EnsureUser(SPHelper.GetAccountName(personInfo.UserName)) : SPHelper.EnsureUser( SPHelper.GetAccountName(personInfo.UserName), SiteUrl); if (userOrGroupToAdd != null) { valueResult = new SPFieldUserValue(item.Web, userOrGroupToAdd.ID, userOrGroupToAdd.LoginName); } } return(valueResult); }
private static SPFieldUserValueCollection FormatUserCollection(IEnumerable <User> usersList, SPListItem item, string siteUrl = null) { var userValueCollection = new SPFieldUserValueCollection(); foreach (User user in usersList) { if (user.IsGroup) { SPGroup group = item.Web.Groups.GetByID(user.ID); if (group != null) { userValueCollection.Add(new SPFieldUserValue(item.Web, group.ID, group.LoginName)); } } else { SPUser spuser = string.IsNullOrEmpty(siteUrl) ? SPHelper.EnsureUser(SPHelper.GetAccountName(user.UserName)) : SPHelper.EnsureUser(SPHelper.GetAccountName(user.UserName), siteUrl); if (spuser != null) { userValueCollection.Add(new SPFieldUserValue(item.Web, spuser.ID, user.UserName)); } else { throw new ArgumentException( string.Format("User {0} not found in the {1} webapplication", user.UserName, item.Web.Title), "usersList"); } } } return(userValueCollection); }