Saturday, August 29, 2009

Checking if user belongs to a group

Scenario:
Often we need to do some action depending upon as if user is part of certain sharepoint group or not.

Solution:
Simple function did the trick

Code:

public bool DoesUserExistInGroup(SPWeb web, SPUser user,string groupName)
{
//Retrieving all the user groups in the site/web
SPGroupCollection userGroups = user.Groups;

foreach (SPGroup group in userGroups)
{
//Checking the group
if (group.Name.Contains(groupName))
{
return true;
}
}
return false;
}

0 comments: