How to delete SharePoint User Profile programmatically - while using FBA

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.", However 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
{
//- Step 1: Delete user from membership
Utility.DeleteMembershipUser(fullUserName);
}
catch (Exception ex)
{
HandleExceptionAsMinor(ex, lblErrorMessage, "deleting the user from Membership Provider", OverrideErrorMessage);
}

bool catchAccessDenied = SPSecurity.CatchAccessDeniedException;
SPSecurity.CatchAccessDeniedException = false;
try
{
//- Step 2: Delete User Profile
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();

/////***** HERE I GET THE ERROR*****/////////////
userProfileManager.RemoveUserProfile(fullUserName);
/////***** HERE I GET THE ERROR ******/////////////
}
}
catch (Exception ex)
{
HandleExceptionAsMinor(ex, lblErrorMessage, "deleting the user from SharePoint Profile", OverrideErrorMessage);
}
finally
{
SPSecurity.CatchAccessDeniedException = catchAccessDenied;
SPContext.Current.Site.AllowUnsafeUpdates = false;
}
try
{
//- Step 3: Delete User from SharePoint Site
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);
} .
parhamazadpeymaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GeorgeGerguesCommented:
I think you better remove the user Profile first , then remove the user.


Also , make sure the account running as Server admin has rights on the profile manager .
that is under the shared services provider - > profile manager rights.



0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
GreatGermCommented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SharePoint

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.