Here's a simple yet useful method for checking membership in a SharePoint group. This method will return true or false to indicate if the current user is a member of the specified SharePoint group in the current site. The current user is obtained from an assumed impersonation context.

public static bool IsUserInGroup(string groupName)
{
    bool inGroup = false;

    try
    {
        SPGroup group = SPContext.Current.Web.Groups[groupName];               
        inGroup = group.ContainsCurrentUser;
    }
    catch (Exception ex)
    {
        // SharePoint throws an exception if the group indexer
        // is not a group the current user belongs to.   
        inGroup = false;
    }
    return inGroup;
}
Bookmark and Share  Comments [2] | Permalink | SharePoint
Tuesday, December 29, 2009 6:24:40 PM (Mountain Standard Time, UTC-07:00)
Where would this code be posted within SharePoint? Can you add this code in the browser or SPD or do you have to use Visual Studio?
Jeanne
Saturday, January 09, 2010 12:30:46 PM (Mountain Standard Time, UTC-07:00)
Jeanne,
The code is C# code intended to be run as part of a Feature or SharePoint deployed to SharePoint Server 2007.

Leave a Comment

Name (required)
Email (will not be published)
Website
 

Comment Preview
Subscribe
Top 5 Posts
Add Google Analytics to a SharePoint Publishing Site
Integrating jQuery 1.3, ASP.NET 3.5 Visual Studio 2008
Add Custom Table Formats to SharePoint Content Editor
Configuring log4net for SharePoint Windows Authentication
Convert Visual Studio 2008 Class Library Project to Web Application Project
Tags
ASP.NET (5) dasBlog (5) GoogleAnalytics (2) jQuery (1) log4net (2) SharePoint (18) WebDesign (3)
 
 
 
Spreadfirefox Affiliate Button
 
Download Notepad++