Using SPUtility.RunWithElevatedPrivileges fails when trying to get SPUser objects from groups. It worked when I removed the privilege block, but this was not practical for non-admin users. The code below worked perfectly for me:
Guid groupWebID = SPContext.Current.Web.ID;
Guid groupSiteID = SPContext.Current.Site.ID;
SPUserToken sysToken = SPContext.Current.Site.SystemAccount.UserToken;
using (SPSite groupSite = new SPSite(groupSiteID, sysToken))
{
using (SPWeb groupWeb = groupSite.OpenWeb(groupWebID))
{
SPGroup group = groupWeb.Groups["My Group"];
foreach (SPUser user in group.Users)
{
AllUsers.Items.Add(user.Email);
}
}
}
No comments:
Post a Comment