If you would like to discuss anything, please contact me via email: Howard.Bayliss@sequence.co.uk

Monday, June 18, 2012

SharePoint check user in group

I created some code to check if the current user belonged to a specific SharePoint group, similar to this:

SPUser user = SPContext.Current.Web.CurrentUser;

foreach (SPGroup group in user.Groups)
{
  if (group.Name == [INSERT YOUR GROUP NAME HERE)
  {
  }
}

This worked when users were directly added to the SharePoint group. However, it did not work if they were part of an AD group, which was then added to the SharePoint group.

To fix the issue, I used code similar to this:

SPGroupCollection groups = SPContext.Current.Site.RootWeb.Groups;
bool inGroup = groups[INSERT YOUR GROUP NAME HERE].ContainsCurrentUser;

No comments:

Post a Comment