Recently while working on custom timer job I ran into a scenario where I need SPUser detail for sending mail from the Person/Group Field value in the SharePoint list. Initially while doing this it would be very simple just by typecasting it, but it doesn’t seem to work.
After reading few article I came to conclusion that to create a SPUser from the value present in the Person/Group field, first we need to create a SPFieldUser object and get the required Field instance and assign to this object, next create a SPFieldUser Value object and assign the value using the GetFieldValue method of the SPFieldUser object. Finally Create a SPUser object and assign user value from the SPFieldUserValue.User.
The complete code snippet for the above logic is given below
SPFieldUser userField = (SPFieldUser)site.Lists["Gifts"].Fields.GetField("AccountOwner");
SPFieldUserValue fieldValue = (SPFieldUserValue)userField.GetFieldValue(item["AccountOwner"].ToString());
SPUser user = fieldValue.User;
string emailId = user.Email;
2 comments:
good post!
Thanks Nathan :)
Post a Comment