Skip to content

Instantly share code, notes, and snippets.

@jmyrland
Created June 5, 2013 13:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmyrland/5713798 to your computer and use it in GitHub Desktop.
Save jmyrland/5713798 to your computer and use it in GitHub Desktop.
SharePoint: is user member of group function.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
namespace Example
{
public class ExamplePage : WebPartPage
{
public static bool IsMemberOfGroup(string currentUser, string groupName)
{
bool isMember = false;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using(SPSite site = new SPSite(SPContext.Current.Site.ID))
{
using(SPWeb web = site.OpenWeb(SPContext.Current.Web.ID))
{
try
{
SPUser user = web.SiteGroups[groupName].Users.GetByID(SPContext.Current.Web.CurrentUser.ID);
if(user != null)
isMember = true;
}
catch
{
isMember = false;
}
}
}
});
return isMember;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment