Sharepoint: Check if a user is member of a group
sharepoint, usergroups
Solution
I have done this by writing an extension method using LINQ. SPGroup inherits from SPPrincipal so you should be able to pass it through to the `principal` parameter:
public static bool Contains(this SPRoleAssignmentCollection rac, SPPrincipal principal)
{
XElement racXml = XElement.Parse(rac.Xml);
return racXml.Elements("permission").Any(vw => (int)vw.Attribute("memberid") == principal.ID);
}
Problem
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.