Click here to Skip to main content
15,887,083 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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,
C#
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);
   }
Posted
Updated 7-Apr-17 3:57am
v2
Comments
Sandeep Mewara 1-Apr-11 6:25am    
Use PRE tags to format code part. It makes the question readable.

"To remove a user profile for another user you must have PortalRight.ManageUserProfile permission."
http://msdn.microsoft.com/en-us/library/microsoft.office.server.userprofiles.userprofilemanager_methods.aspx[^]

Are you sure the proper right have been granted.

When using RunWithElevatedPrivileges the account configured for the Application Pool of the Web Application the code is running in is used. If you are running this from Central Admin it would the account setup of for that.
 
Share this answer
 
Comments
[no name] 18-Apr-11 20:07pm    
Exactly the problem is that my account doesn't have enough access on Central Admin.
Basically, I need to develop a WebPart with which "My Application Administrations" will be able to delete users and consequently profiles as well. Due to using FBA, these users will not have any access to Central Admin, so they are not able to delete SharePoint User Profiles. However, this WebPart is working as it is expected when I use Windows Authentication Portals (provided that the user has got enough access....).
My question is how I can bypass this scenario and let the "Application Administrator", who potentially are not site collection admin or whatsoever, delete users form their site. This deletion needs to be done completely everywhere. That's why I'm looking for a solution to kind of delete SharePoint Users' Profile. :-?

Your hint was so fantastic and hit the point. :) Good job, man!

Cheers mate,
Parham
Manage to fix the code by

  SPSecurity.RunWithElevatedPrivileges(delegate()
           {
string AccountName="yourAccountNamehere";
                using (SPSite site = new SPSite(context.GetUrl()))
                {
                    SPServiceContext current = SPServiceContext.GetContext(site);
                    UserProfileManager upm = new UserProfileManager(current);

                    if (upm.UserExists(AccountName))
                    {

                        ProfileBase profile = upm.GetUserProfile(AccountName);
                        upm.RemoveProfile(profile);                    
                       
                    }
                    else
                    {
                        return;
                    }
                }
           });
 
Share this answer
 
v2
Comments
Richard Deeming 7-Apr-17 10:16am    
This question was asked, answered, and solved SIX YEARS AGO.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900