Wednesday, 18 May 2011

Determine if your SPFieldUserValue is a user or group

SPFieldUserValue field = new SPFieldUserValue(web, item["AssignedTo"].ToString());

 // Test for a user or group value (null indicates a group)
      if (field.User != null)
       {
         //user
       }
      else
       {
         //group
        }

1 comment:

  1. A user who has been removed from a site will also come back with field.User as null, sending you into the group code.

    And another gotcha that made things worse, web.Groups.GetByID doesn't check that a group exists before trying to get the group and errors out.

    As far as I can tell, the best option is to use a method to check if the group exists before you try to get it. And if it doesn't assume it is a removed user or group.

    ReplyDelete

Comments are moderated, so you'll have to wait a little bit before they appear!