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.

Advertisements

~ by Neha Sinha on March 28, 2008.

23 Responses to “Accessing the SPUser from SPFieldUser”

  1. Thank you very much, I needed this!

  2. I also want to thank you for this. Very helpful!

  3. Ta, just what I was looking for!

  4. Amazing, Thank you very much.

  5. 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!

  6. 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 😦

  7. @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);
    }

  8. 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.

  9. Thank you, helped me a bunch!

  10. absolutely perfect. thanks so much 🙂

  11. if you want the sharepoint to lookup for the username,
    use “-1;#domain\user” format

  12. 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.

  13. No, I don’t use SPFieldUserValueCollection. I use SPFieldUserValue just like the example from above

  14. I resolved my problem. Thanks for help anyway

    Regards

  15. Thanks for this, but does any one know what does “1#;” stands for?

  16. I am new to Sharepoint. I inserted the “Assign To” lookup in the body of my workflow email and it obviously displayed the NT username (SPFieldUser???). How do I use the codes provided above in order to return the SPUser? (a workflow variable? a calculated field in the Task List?) Thanks!

  17. I just bookmarked ur blog..awesome work Neha…:-)..

  18. Thanks, this save my life and sanity… kkkk

  19. Can you please also suggest how to update this SPUserField in any list ?
    for example I have to update it in Attendee list.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: