Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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

                                                                }
                                                            }


                                                        }
                                                    }
                                                });
share|improve this question
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

1 Answer 1

up vote 4 down vote accepted

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)
    {
     // ...
    } 
    
    
share|improve this answer
    
Thanks so much! Done! –  user1200566 Dec 6 '12 at 18:16

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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