Extension method to get SPUser object from SPListItem person field


/ Published in: C#
Save to your folder(s)



Copy this code and paste it in your HTML
  1. public static SPUser GetUser(this SPListItem item, string displayName)
  2. {
  3. //get field and cast to User Field type
  4. var field = item.Fields.GetField(displayName) as SPFieldUser;
  5.  
  6. if (null == field)
  7. {
  8. throw new ApplicationException("This field is not a User!");
  9. }
  10.  
  11. var fieldValue = field.GetFieldValue(item[field.Id].ToString()) as SPFieldUserValue;
  12.  
  13. SPUser user = null;
  14. if (null != fieldValue)
  15. {
  16. user = fieldValue.User;
  17. }
  18.  
  19. return user;
  20. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.