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

I am trying to create a site and add users to its group. it creates the site successfully but when I try to add users to groups I get Access denied and no user has been added to groups here is my code:

 SPSecurity.RunWithElevatedPrivileges(delegate()
        {
            using (SPSite site = new SPSite(newWebUrl))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    foreach (string gName in pPickers.Keys)
                    {
                        web.EnsureUser(web.CurrentUser.LoginName);
                        PeopleEditor peopleEditor = pPickers[gName];

                        foreach (PickerEntity entity in peopleEditor.ResolvedEntities)
                        {
                            string groupName = gName + " on " + web.Title;

                            SPGroup group = web.Groups[groupName];
                            SPUser user = web.EnsureUser(entity.Key);

                            if (user != null)
                            {
                                group.AddUser(user);
                                group.Update();
                            }
                        }
                    }
                }
            }
        });
share|improve this question
add comment

3 Answers

can you try this:

using (SPWeb web = site.OpenWeb(SPContext.Current.Web.ID)) 

instead of:

using (SPWeb web = site.OpenWeb()) 

this will explain why your code is failing :)

http://blogs.msdn.com/b/sasohail/archive/2010/10/31/access-denied-within-spsecurity-runwithelevatedprivileges.aspx

another method is to use this:

http://sarangasl.blogspot.co.uk/2009/12/add-user-to-sharepoint-user-group.html

hope it helps :)

share|improve this answer
    
thank you. about your first explanation, I am not trying to add users to a group of current.web but the spweb that I just created. so site.OpenWeb(SpContenxt.Current.Web.Id) returns the parent web. doesn't it? and the second solutin gave same error access denied. –  Medes Oct 4 '12 at 15:04
    
I want to let you know that your answer was 50% right. because I need to use RunWithEvlevatedPrivileges earlier. I mean before creating SPSite instance which reprecent parent web. –  Medes Oct 5 '12 at 8:17
add comment

You are most likely receiving this error because the group membership can only be modified by the group owner. If you open the Group settings page - you will see a setting that governs who can edit the group membership. There are two options; Group owner and Group members. In the object model it is governed by the Boolean property SPGroup.AllowMembersEditMembership

Running with elevated privileges means that you are trying to modify the group membership in the context of the application pool account.

The solution is to take the SPUserToken of the group owner and create an impersonated context passing the user token into the overloaded SPSite constructor.

share|improve this answer
    
Thank you for your answer. it was access denied because the user who tried to create subsite had read rights. I solved it by adding RunWithElevatedPrivileges one step earlier. I mean before creating parent SPSite. –  Medes Oct 5 '12 at 8:16
add comment

Try the following code. It is always better to use the token of the system account instead of running all your code inside of SPSecurity.RunWithElevatedPriviledges(). The code is much stable and secure this way.

UPDATE: Since you are using your code in an Application Page, make sure you have called the SPUtility.ValidateFormDigest() method before any of your elevated code. If that does not work, try setting the web.AllowUnsafeUpdates = true of the newly created web.

SPUtility.ValidateFormDigest();

SPUserToken sysToken = SPContext.Current.Site.SystemAccount.UserToken;
using (SPSite site = new SPSite(newWebUrl,sysToken))
{
    using (SPWeb web = site.OpenWeb())
    {
        foreach (string gName in pPickers.Keys)
        {
            web.EnsureUser(web.CurrentUser.LoginName);
            PeopleEditor peopleEditor = pPickers[gName];

            foreach (PickerEntity entity in peopleEditor.ResolvedEntities)
            {
                string groupName = gName + " on " + web.Title;

                SPGroup group = web.Groups[groupName];
                SPUser user = web.EnsureUser(entity.Key);

                if (user != null)
                {
                    group.AddUser(user);
                    group.Update();
                }
            }
        }
    }
}
share|improve this answer
    
it generated same error. I have created an ApplicationPage which you use to create subsites under site collection. Everybody is reader on site collection but can see the custom action which opens the ApplicationPage and creates subsite and adds all users in PeoplePickers to proper group. directly after the subsite has been created I call this function (above code) to add users to proper groups. the problem is neither sysToken (your code) an ElevatedPrivileges using loggod in user to run the code. –  Medes Oct 5 '12 at 7:43
    
If you are using an application page, make sure you have added the SPUtility.ValidateFormDigest(); method before any of your elevated code. Have a look at the updated code. –  Vardhaman Deshpande Oct 5 '12 at 9:38
    
Thanks yes I have added it. –  Medes Oct 5 '12 at 9:49
add comment

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.