Hi guys,
I'm trying to develope a webpart with which "Admins" can to add, edit and delete users both in SharePoint and SQL-Membership.
This webpart has to work in a FBA portal.
Here, I have a problem with deleting SharePoint User profiles. My code is as below. But this code is working in just in Windows Authentication portals, not Form Based Authentication portals.
I get an error message when I log in to the portal with a SQL-Membership User.
Here is the error message:"Access Denied: Only an administrator may delete a user profile.", How ever I am using
RunWithElevatedPrivileges
!
It seems that
SPSecurity.RunWithElevatedPrivileges
does not work in SPFarm?
I don't know how I can get rid of this error.:(
Is there anyone who can help me?
Cheers,
SPSecurity.RunWithElevatedPrivileges(
delegate
{
try
{
Utility.DeleteMembershipUser(fullUserName);
}
catch (Exception ex)
{
HandleExceptionAsMinor(ex, lblErrorMessage, "deleting the user from Membership Provider", OverrideErrorMessage);
}
bool catchAccessDenied = SPSecurity.CatchAccessDeniedException;
SPSecurity.CatchAccessDeniedException = false;
try
{
SPUtility.ValidateFormDigest();
SPContext.Current.Site.AllowUnsafeUpdates = true;
var sc = ServerContext.GetContext(site);
var userProfileManager = new UserProfileManager(sc);
if (userProfileManager.UserExists(fullUserName))
{
UserProfile userProfile = userProfileManager.GetUserProfile(fullUserName);
if (userProfile.PersonalSite != null)
userProfile.PersonalSite.Delete();
userProfileManager.RemoveUserProfile(fullUserName);
}
}
catch (Exception ex)
{
HandleExceptionAsMinor(ex, lblErrorMessage, "deleting the user from SharePoint Profile", OverrideErrorMessage);
}
finally
{
SPSecurity.CatchAccessDeniedException = catchAccessDenied;
SPContext.Current.Site.AllowUnsafeUpdates = false;
}
try
{
site.RootWeb.SiteUsers.Remove(fullUserName);
}
catch (Exception ex)
{
HandleExceptionAsMinor(ex, lblErrorMessage, "deleting the user from Site Groups", OverrideErrorMessage);
}
}
});
string connectionString = ConfigurationManager.ConnectionStrings[ConnStringName].ConnectionString;
Grid.DataBind();
}
catch (Exception ex)
{
HandleExceptionAsMinor(ex, lblErrorMessage, "deleting the user", OverrideErrorMessage);
}