How to get SPUser (User) from Person or Group field of Sharepoint List
Posted by namwar on July 26, 2008
While writing document event handler, sometimes we need to to process the logic based on the value of Person or Group field. Sharepoint stores ID of the user in Person or Group field and there is no obvious way to get the SPUser object from this ID because this ID is stored as ID field in the userinfo and userdata tables. Following function is the qucikest way to get SPUser object from ID. Call this function with
- the name of the Sharepoint list where your event is fired and
- SPItemEventProperties object which has been passed to you as your event arguments
public SPUser GetSPUserFromID(string listName, SPItemEventProperties properties) { SPFieldUser userField = (SPFieldUser)properties.OpenWeb().Lists[listName].Fields.GetField(accountNameField); SPFieldUserValue fieldValue = (SPFieldUserValue)userField.GetFieldValue(currentValue); SPUser user = fieldValue.User; return user; }
Chris Sammut said
Hi,
I wish to extract all the users from a User field with multiple selection in an event handler…
I did somethin like this but it doesnt seem to work.
SPUserCollection userVals = (SPUserCollection)properties.AfterProperties[“Team_x0020_Leader”];
foreach (SPUser userVal in userVals)
{
string TLeaderName = userVal.Name.ToString();
string TLeaderEmail = userVal.Email.ToString();
string TLeaderLogin = userVal.LoginName.ToString();
SPRoleAssignment roleAssignment1 = new SPRoleAssignment(TLeaderLogin, TLeaderEmail, TLeaderName, “notes”);
SPRoleDefinition RoleDefinition1 = newWeb.RoleDefinitions.GetByType(SPRoleType.Administrator);
roleAssignment1.RoleDefinitionBindings.Add(RoleDefinition1);
//Check inheritance for Project Manager
if (!newSubWeb.HasUniqueRoleAssignments)
{
newSubWeb.BreakRoleInheritance(true);
}
newSubWeb.RoleAssignments.Add(roleAssignment1);
}
any idea wat Im doing worng?
Thank you
Reg said
Nice, but where did you get “currentValue” from?
o'g said
rock on…great solution. to answer a previous post…the ‘current value’ is the name of the user. i’ve implemented this a little differently, that is, not in an event handler. i am populating a grid based on a list and i display the ‘users’ name (user is a column type in the list i am iterating and binding to the grid) as a mailto link. here’s the code…
for each item in list Gifts…
SPFieldUser userField = (SPFieldUser)site.Lists[“Gifts”].Fields.GetField(“Recipient Name”);
SPFieldUserValue fieldValue = (SPFieldUserValue)userField.GetFieldValue(item[“Recipient Name”].ToString());
SPUser user = fieldValue.User;
giftRow[6] = user.Email;
works perfectly. thanks!
Raja said
Great Job, It Works great
SharePoint Steve » Getting the SPUser object out of a person or group list field said
[…] decided to share. Nothing that I found online gave me the exact answer I was looking for, although this article got me headed in the right direction. Below is the code to grab the email stored in a […]
wael said
Wonderful, I search for hours on how to get the SPUser from the UserId. Thanks
Jim Adcock said
@Reg –
string currentValue = item[field.Title].ToString();
ref –
http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/955d0796-24c5-4725-b107-d1d8ca4e0241
Fabian said
Great post, tnks for help.
CodeWarrior said
One thing to note. If your “User or Group” column allows for multiple users to be selected, you will want to use SPFieldUserValueCollection instead of SPFieldUserValue. i.e.
SPFieldUser UsersColumn = (SPFieldUser)currentItem.Fields.GetField(“Users”);
SPFieldUserValueCollection Users = (SPFieldUserValueCollection)UsersColumn.GetFieldValue(currentItem[“Users”].ToString());