How to get SPUser (User) from Person or Group field of Sharepoint List

By Namwar Rizvi - Last updated: Saturday, July 26, 2008 - Save & Share - 10 Comments

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

  1. the name of the Sharepoint list where your event is fired and
  2. 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;
}
Share
Posted in Event Handler • Tags: , Top Of Page

10 Responses to “How to get SPUser (User) from Person or Group field of Sharepoint List”

Comment from Chris Sammut
Time August 4, 2008 at 10:02 am

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

Comment from Reg
Time January 22, 2009 at 10:33 am

Nice, but where did you get “currentValue” from?

Comment from o’g
Time April 21, 2009 at 6:36 pm

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!

Comment from Raja
Time August 6, 2009 at 9:56 am

Great Job, It Works great

Pingback from SharePoint Steve » Getting the SPUser object out of a person or group list field
Time September 3, 2009 at 6:48 pm

[…] 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 […]

Comment from wael
Time October 15, 2009 at 12:17 pm

Wonderful, I search for hours on how to get the SPUser from the UserId. Thanks

Comment from Jim Adcock
Time April 15, 2010 at 4:25 pm

@Reg –

string currentValue = item[field.Title].ToString();

ref –
http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/955d0796-24c5-4725-b107-d1d8ca4e0241

Comment from Fabian
Time May 28, 2010 at 4:33 pm

Great post, tnks for help.

Comment from CodeWarrior
Time June 16, 2010 at 2:28 pm

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());

Comment from Sourcecode
Time May 26, 2012 at 4:09 pm

This code rocks. It works with SP2010 too!

Write a comment