Created
June 5, 2013 13:19
-
-
Save jmyrland/5713798 to your computer and use it in GitHub Desktop.
SharePoint: is user member of group function.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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