Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sharepoint: Check if a user is member of a group

how can I check if a user (not the one currently logged in) is member of a certain group? Trying to retrieve a user from a group of which he's not a member leads to an SPException, so checking for null is not possible.

So how would you solve this problem. At the moment I think about searching in the SPGroup.Users.XML string for the user's name or iterating over all the group members and checking the login names.

Update: I forgot to mention that I want to avoid the usage of exception handling to check the user's membership.

like image 862
Flo Avatar asked Jun 30 '09 14:06

Flo


People also ask

What is the difference between a member and an owner in SharePoint?

For example, the Members group has the Contribute permission level by default. As a site owner, you choose which permissions are associated with each permission level (except for Limited Access and Full Control, which cannot be customized) or add new permission levels to combine different sets of permissions.


1 Answers

Create an Extension class for SPUser and static method:

public static class SPUserExtension {
   public static bool InGroup(this SPUser user, SPGroup group)
      {
        return user.Groups.Cast<SPGroup>()
          .Any(g => g.ID == group.ID);
      }
   }
}

Then invoke this method on your SPUser object:

SPUser user;
SPGroup group;
//...
bool isMember = user.InGroup(group);
like image 69
Anatoly Mironov Avatar answered Nov 08 '22 11:11

Anatoly Mironov