Feb
22
2010

How to check whether a SharePoint user is in a particular group

Here’s a quick function I wrote to check whether a user is a member of a particular SharePoint group:

private bool IsMemberOf(string groupName)
{
    SPUser user = SPContext.Current.Web.CurrentUser;
    try
    {
        if (user.Groups[groupName] != null)
            return true;
        else
            return false;
    }
    catch
    {
        return false;
    }
}

The try-catch is required as – somewhat counter-intuitively – SharePoint seems to throw a “Group not found” error if the user is not a member of the group.

If you enjoyed this post, please share it:
  • Twitter
  • Facebook
  • del.icio.us
  • Reddit
  • Digg
  • StumbleUpon
  • Technorati
  • email
  • RSS

Written by Chris Barnes in: ASP.NET | Tags: , , , , ,

No Comments »

RSS feed for comments on this post. TrackBack URL


Leave a Reply

Proudly powered by WordPress and W2-S Internet Services. Design based on Aeros Theme by TheBuckmaker.com.