Could anyone explain me why when I trying to loop user of a group
foreach (var u in group.Users){....},
I get the error :
Attempted to perform an unauthorized operation.
I notice that inside group.User.Count I have this exception:
group.Users.Count' threw an `exception of type
'System.UnauthorizedAccessException' int {System.UnauthorizedAccessException}`
Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(
delegate()
{
using (SPSite scsite = new SPSite(SPContext.Current.Site.Url))
{
using (SPWeb scweb = scsite.OpenWeb())
{
foreach (var g in SPContext.Current.Web.Roles.Web.Groups)
{
SPGroup group = (SPGroup)g;
if (group.Name.ToLower().Contains("proprietari"))
{
foreach (var u in group.Users)
{
SPUser user = (SPUser)u;
if (user.Name.Equals(SPContext.Current.Web.CurrentUser.Name))
{
a = true;
}
}
}
}
}
}
});