Accessing the SPUser from SPFieldUser
SPFieldUser field type stores the username in “1#;User” format.
For accessing the user data (e.g, parsing “Assigned To” field of a Tasks list item in SharePoint) in a better way, the code below will allow you to quickly get the underlying SPUser object.
In this example, we have a task list called “SampleTasksList”, the SPFieldUser is the “Assigned To” column -
using (SPSite site = new SPSite(“http://mysharepointsite.com”))
{
using (SPWeb web = site.OpenWeb())
{
SPList taskList = web.Lists["SampleTaskList"];
SPListItem taskListItem = taskList.Items[0];
SPFieldUser assignedTo = (SPFieldUser) taskListItem.Fields[SPBuiltInFieldId.AssignedTo];
SPFieldUserValue user= (SPFieldUserValue)assignedTo.GetFieldValue(taskListItem[SPBuiltInFieldId.AssignedTo].ToString());
SPUser userObject = user.User;
}
}
The userObject will provide you with all the information related to the user in the “Assigned To” field for a particular task in the Tasks list.




Thank you very much, I needed this!
I also want to thank you for this. Very helpful!
Ta, just what I was looking for!
Amazing, Thank you very much.
Unbelievable! What MS puts you through to do something that should be simple. A thousand thanks! I’ve been looking for this for quite a while. Get SPUser from a UserInfo field type!
Hi, nice job. I’m looking for an answer :S
Do you know what should I do to save a user into a User or Group field ? I’m trying from my C# code, but I always get an error
@dEiBeAt : You can use the below code to add users to a user group.
PeopleEditor ppl;
PickerEntity objEntity;
objEntity = (PickerEntity)ppl.ResolvedEntities[0];
SPUserInfo objInfo = new SPUserInfo();
objInfo.LoginName = objEntity.Key;
strUserName = objEntity.DisplayText;
strLoginName = objInfo.LoginName;
strEmail = objEntity.EntityData["Email"].ToString();
strName = objInfo.Name;
strNotes = objInfo.Notes;
SPUserCollection userColl = SPContext.Current.Web.Groups["My Group"].Users;
ppl.ValidateEntity(objEntity);
if ((strLoginName != string.Empty) && (strEmail != string.Empty) && (strName != string.Empty) && (strNotes != string.Empty))
{
userColl.Add(strLoginName, strEmail, strName, strNotes);
}
Cheers for this. Worked great for getting the Created By field. Couldn’t use the SPBuiltInFieldId.Created_x0020_By but using “Created by” worked fine.
Thank you, helped me a bunch!
absolutely perfect. thanks so much
if you want the sharepoint to lookup for the username,
use “-1;#domain\user” format
I get an error: Unable to cast object of type ‘Microsoft.SharePoint.SPFieldUserValueCollection’ to type ‘Microsoft.SharePoint.SPFieldUserValue’
Why is that?
Thanks
@Zarko : SPFieldUserValueCollection is a grp of user values and i guess u r trying to convert it to a single user value.
No, I don’t use SPFieldUserValueCollection. I use SPFieldUserValue just like the example from above
Can u plz show the code snippet?
I resolved my problem. Thanks for help anyway
Regards
Thanks for this, but does any one know what does “1#;” stands for?