I had a question today about
how to determine the email address of the user who is mentioned in the "Responsible Field" of a SharePoint list. He was trying to develop an event receiver that would email a particular user if they were mentioned in that field.
To this end, here is a tiny method I wrote to get an SPUser based on a Person field in a SharePoint list. The code has to first obtain a reference to the relevant SPField and then use the "GetFieldValue" method of the SPField to get the SPFieldUserValue. The SPFieldUserValue is really just an SPUser:
///
/// Gets the SP User object from a person field (e.g. the Modified By field)
/// so we can determine the email address or other details of that user.
///
/// e.g. the SPItem with the Person field obtained
/// from web.Lists[0].Items[0] /// e.g. a person field e.g. "Modified By"
public static SPUser GetSPUserFromPersonField(SPListItem listItem, string fieldName)
{
var personField = listItem.Fields[fieldName];
return ((SPFieldUserValue)personField.GetFieldValue(listItem[fieldName].ToString())).User;
}
DDK
No comments:
Post a Comment