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