Pull users and groups from a SP list that allows multiple users and groups.

This is a follow up post from a previous post :

http://surfpointtech.com/2011/09/12/how-to-pull-the-email-addresses-from-a-sharepoint-user-field-allow-multiple-users/

When examining a User field on a SharePoint list, you need to see whether the list setting allows for multiple users as well as both Users and Groups.

The key to pulling both the groups and the users is to be able to test for a group as opposed to a user object.  In this case, I found that the fieldUser.User object is null for groups, but not for Users.  So, as you iterate through the fieldUser objects in the SPFieldUserValueCollection, you’ll find a group when the fieldUser is not null, but the fieldUser.User object is null.  That way, you know you have a SPGroup at that point, and can loop through the users in the group.

After that, you do the normal adding of each fieldUser.User.Email

I [...]