This small method will return you the SPUser from any ListeItem. The second parameter is the column which contains the User. In my next snippet I will provide the method for the collection of users.
#region Get SP User private SPUser GetSPUser(SPListItem item, string ColumnName) { SPFieldUser field = item.Fields[ColumnName] as SPFieldUser;
if (field != null && item[ColumnName] != null) { SPFieldUserValue fieldValue = field.GetFieldValue(item[ColumnName].ToString()) as SPFieldUserValue; if (fieldValue != null) { return fieldValue.User; } } return null; } #endregion
|
Same piece of code, not in a method
SPFieldUser userFieldOwnedBy = (SPFieldUser)oWeb.Lists[“ListName”].Fields.GetField(“FieldAssignedTo”); SPFieldUserValue filedValueOwnedBy = (SPFieldUserValue)userFieldOwnedBy.GetFieldValue(“FieldAssignedTo”); SPUser userAssignedTo = filedValueOwnedBy.User; |
Thanks for reading. If you have some other explanation – please post a comment… I’ll be happy to hear.
...HaPpY CoDiNg
Partha (Aurum)
I dont know is this SP 2007 code !? is it difference from sp2010 code ! i have error on GetFieldValue(“FieldAssignedTo”); the documentation in msdn for this method (getFieldValue) its need the value as string its dont take the field name,,, if i have the value as string , why i need it as object !? #fail #Microsoft
ReplyDeleteChange => item.Fields[ColumnName]
ReplyDeleteTo => .Fields.GetField(ColumnName)