Posted By

dkitchen on 01/28/10


Tagged


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

digiteyes


Extension method to get SPUser object from SPListItem person field


 / Published in: C#
 

  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  

You need to login to post a comment.