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!
kelthar said this on May 22, 2008 at 6:25 pm |
I also want to thank you for this. Very helpful!
David Remillard said this on July 9, 2008 at 2:50 am |
Ta, just what I was looking for!
Ross said this on July 23, 2008 at 4:24 pm |
Amazing, Thank you very much.
Amit said this on July 24, 2008 at 4:59 pm |
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!
Bob said this on September 9, 2008 at 2:17 am |
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 said this on September 17, 2008 at 7:51 pm |
@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);
}
Neha Sinha said this on September 18, 2008 at 9:30 am |
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.
Nicky said this on October 1, 2008 at 8:38 pm |
Nicky, You can use SPBuiltInFieldId.Author instead of SPBuiltInFieldId.Created_x0020_By. See my post http://discoverlars.wordpress.com/2011/03/06/sharepoint-built-in-fields
Lars Nielsen said this on March 7, 2011 at 1:16 am |
Thank you, helped me a bunch!
Jan said this on December 19, 2008 at 10:05 pm |
absolutely perfect. thanks so much 🙂
Dave F said this on February 27, 2009 at 2:22 am |
if you want the sharepoint to lookup for the username,
use “-1;#domain\user” format
Vijay said this on April 8, 2009 at 2:56 am |
I get an error: Unable to cast object of type ‘Microsoft.SharePoint.SPFieldUserValueCollection’ to type ‘Microsoft.SharePoint.SPFieldUserValue’
Why is that?
Thanks
Zarko said this on June 9, 2009 at 3:46 pm |
@Zarko : SPFieldUserValueCollection is a grp of user values and i guess u r trying to convert it to a single user value.
Neha Sinha said this on June 11, 2009 at 10:54 pm |
No, I don’t use SPFieldUserValueCollection. I use SPFieldUserValue just like the example from above
Zarko said this on June 12, 2009 at 12:31 pm |
Can u plz show the code snippet?
Neha Sinha said this on June 26, 2009 at 1:15 pm |
I resolved my problem. Thanks for help anyway
Regards
Zarko said this on June 26, 2009 at 3:39 pm |
Thanks for this, but does any one know what does “1#;” stands for?
Cj said this on August 13, 2009 at 10:29 am |
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!
ClimbingEden said this on February 6, 2010 at 12:59 am |
I just bookmarked ur blog..awesome work Neha…:-)..
Kish said this on July 23, 2011 at 4:58 pm |
Welcome here Kishore 🙂
Neha Sinha said this on July 24, 2011 at 11:49 am |
Thanks, this save my life and sanity… kkkk
erick souza said this on December 1, 2011 at 3:40 am |
Can you please also suggest how to update this SPUserField in any list ?
for example I have to update it in Attendee list.
Rajesh Soni said this on May 25, 2012 at 4:50 pm |