2

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;
                                                                        }
                                                                    }

                                                                }
                                                            }


                                                        }
                                                    }
                                                });
  • 2
    If you want to check if the user exists in Group or not, you can simply use SPGroup.ContainsCurrentUser – Arsalan Adam Khatri Dec 5 '12 at 15:55
  • Does the user you're running this code as have permission? – Kit Menke Dec 5 '12 at 16:00
  • Thanks so much! But, can you see in my code , why I can't loop group.user if I'm executing inside "RunWithElevatedPrivileges()" ? – user1200566 Dec 5 '12 at 16:01
4

You can use SPContext within a "RunWithElevatedPrivileges" block, but objects you get from "SPContext" such as: SPUser, SPWeb, etc. are not running in elevated privilege, they just take the current user security context.

  • So why not just use "scweb" to get to the "Group". Since "scweb" object is initialized within the "RunWithElevatedPrivileges" block just try this instead:

    foreach (var g in scweb.Groups)
    {
     // ...
    } 
    
    
|improve this answer|||||
  • This solution does not work! I'm doing the same as you proposed, but get also unauthorized message. Any different approach? – dns_nx Sep 21 '17 at 5:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.