Getting the SPUser out of SPFieldUser

SPFieldUser field type stores the username in “1#;User” fashion. This is a small annoyance when trying to access the user data, for instance when parsing “Assigned To” field of a Tasks list item.

This small amount of code will allow you to quickly get the underlying SPUser object. In this example, the SPFieldUser is the “Assigned To” column.

SPFieldUser assignedTo = (SPFieldUser)task.Fields[SPBuiltInFieldId.AssignedTo];

SPFieldUserValue user = (SPFieldUserValue) assignedTo.GetFieldValue(task[SPBuiltInFieldId.AssignedTo].ToString());

SPUser userObject = user.User;

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.