Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Access Denied adding member to SP Group

Status
Not open for further replies.

JJgotMail

Programmer
Dec 26, 2000
2
US
Hello,

I have created a C# web part that adds a user found in Active Directory to an existing SharePoint Group. It runs fine if I am running as the SharePoint administrator but gives me an access denied message if I try running the Add as a different user who has full control to the site.

I was able to fake out the "Search" function by changing the group.owner to the ID of the logged on user but the "CanCurrentUserEditMembership" doesn't fall for that.

I have the code encapsulated with RunWithElevatedPrivileges and web.AllowUnsafeUpdates = true and it sets my ID to SharePoint\System but still tells me Access Denied on the Group.AddUser. This link sure made it sound easy
I have now tried impersonating the administrator account but still getting access denied.

I am running in my own VM environment and I am wondering if there is a patch or something I am missing. It won't look pretty but here is the snippet:

foreach (PickerEntity entity in PeopleEditor.ResolvedEntities)
{
if (entity.IsResolved == true)
{
try
{

SPSecurity.RunWithElevatedPrivileges(delegate()

{
SPSite oSite = null;
oSite = new SPSite(SPContext.Current.Site.ID);
SPWeb oweb = oSite.OpenWeb();


SPUser SAuser = oweb.AllUsers["MYDOMAIN\\Administrator"];

SPUserToken token = SAuser.UserToken;
SPSite impersonatedSiteCollection = new SPSite(SPContext.Current.Site.ID, token);


using (SPWeb web = impersonatedSiteCollection.OpenWeb(oweb.ID))
{

web.AllowUnsafeUpdates = true;

//RequestList is a SharePoint List
//_ddl_ReqList_Assoc_Groups is a drop down of SP groups associated with that list
SPGroup group = RequestsList.Lists.Web.Groups[_ddl_ReqList_Assoc_Groups.SelectedItem.Value];


group.AddUser(entity.Key, null, null, null);
group.Update();
label.Text = "The selected viewer: " + entity.DisplayText + " has been added to the : " + group.Name + " location</br>";
this.buttonAddUser.Enabled = false;

}

}
);
}
 
Additionally:

When I use the elevated web it is not finding the Group. The elevated web is being set to the site collection. The groups I am targeting are only defined under the subsite where the web part is running. I am new to SharePoint so what I just said might not make sense. But there are 4 groups defined in the elevated web "Groups" but there are an additional 6 groups when run using my original code: SPGroup group = RequestsList.Lists.Web.Groups[_ddl_ReqList_Assoc_Groups.SelectedItem.Value];

I should mention that the subsite permissions page states "This Web site does not inherit permissions from its parent". On the site permissions page I see all my targeted groups. When I go up to the site collection and go to Site Permissions, I see the 4 groups that I see in the debugger running with elevated privileges and impersonating the administrator.

Latest code snippet.

foreach (PickerEntity entity in peopleEditor.ResolvedEntities)
{
if (entity.IsResolved == true)
{
try
{

SPSecurity.RunWithElevatedPrivileges(delegate()

{


using (SPWeb elevatedWeb = new SPSite(SPContext.Current.Site.ID).OpenWeb())

{


elevatedWeb.AllowUnsafeUpdates = true;

SPUser NewUser = elevatedWeb.EnsureUser("MyDomain\\Administrator");


SPGroup group = elevatedWeb.Groups[_ddl_ReqList_Assoc_Groups.SelectedItem.Value];


group.AddUser(entity.Key, null, null, null);
group.Update();

label.Text = "The selected viewer: " + entity.DisplayText + " has been added to the : " + group.Name + " location</br>";
this.buttonAddUser.Enabled = false;


}

}
);
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top