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; }
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
Nice, but where did you get “currentValue” from?
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!
Great Job, It Works great
Wonderful, I search for hours on how to get the SPUser from the UserId. Thanks
@Reg –
string currentValue = item[field.Title].ToString();
ref –
http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/955d0796-24c5-4725-b107-d1d8ca4e0241
Great post, tnks for help.
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());
This code rocks. It works with SP2010 too!