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

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

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

  1. 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

  2. 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!

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

Comments are closed.

Share via
Copy link
Powered by Social Snap