none
Unable to delete items from SiteUserInfoList

    Question

  • Hi all,

    I have been trying to write a job that removes redundant entries from the SiteUserInfoList, but I am getting the following exception whenever I attempt to remove an item:

    Exception – Microsoft.Sharepoint.SPException

    Error Message: Cannot complete this action. Please try again

    Error code: -2147467259


    I am able to add new items, and to update existing items on this list.

    I am using the following code:-

            private void DeleteSiteUsers(string url, string sspUrl)
            {
                WindowsImpersonationContext wic = null;
                wic = WindowsIdentity.GetCurrent().Impersonate();
    
                using (SPSite site = new SPSite(url))
                {
                    site.ReadOnly = false;
                    using (SPWeb web = site.RootWeb)
                    {
                        web.AllowUnsafeUpdates = true;
                        SPList userList = web.SiteUserInfoList; //RetrieveUserListFromSharepoint(web);
                        UserProfileManager userProfileManager = RetrieveUserProfileManager(sspUrl);
    
                        List<int> deletedItemIds = new List<int>();
    
                        //web.Site.RootWeb.SiteUsers.Remove(UserId)
                        userList.AllowDeletion = true;
                        userList.CheckPermissions(SPBasePermissions.DeleteListItems);
                        
                        for (int i = 0; i < userList.Items.Count; i++)
                        {
                            SPListItem user = userList.Items[i];
                            if (!DoesUserExistInSSP(user, userProfileManager))
                            {   
                                deletedItemIds.Add(user.ID); 
                            }
                        }
                        DeleteUserItems(ref userList, deletedItemIds);
                        web.AllowUnsafeUpdates = false;
                    }
                }
    
                wic.Undo();
                wic.Dispose();
            }
    
    
            private void DeleteUserItems(ref SPList users, List<int> deletedItemIds)
            {                                 
                foreach (int id in deletedItemIds)
                {
                    try
                    {
    //THIS IS WHERE THE ERRORS ARE OCCURING. users.Items.DeleteItemById(id); } catch (Exception ex) { } } }

    These methods are being called with RunWithElevatedPriveledges

    As you can see I have tried most things, and none work.  I am running this as a console application at first, which is running as a sharepoint adminsitrator.

    This is running on a virtual PC 'copy' of the live system, so the VPC does not have access to any of the AD controllers etc.  (I am not sure if this will make any difference)

    I have googled this, but I have found no results that can fix this.

    Any ideas?

    Tuesday, April 07, 2009 10:19 AM

Answers

  • If you are trying to remove users from site collection, use SPWeb.SiteUsers.RemoveByID(id); method. This method will remove the user from site collection.
    -Sridhar http://sridharu.blogspot.com
    Tuesday, April 07, 2009 11:36 AM

All replies

  • If you are trying to remove users from site collection, use SPWeb.SiteUsers.RemoveByID(id); method. This method will remove the user from site collection.
    -Sridhar http://sridharu.blogspot.com
    Tuesday, April 07, 2009 11:36 AM
  • Thanks for the reply, but sadly I only wish to delete the list item, and not the user, as the list should should only show the currently active users (i.e. the ones that are currently able to log in).

    Tuesday, April 07, 2009 11:52 AM
  • User Info list maintains the user information who has access to the site collection, so if you are trying to remove from this list means you are removing the user from site collection.

    -Sridhar http://sridharu.blogspot.com
    Tuesday, April 07, 2009 3:09 PM
  • Thanks for that info.  What would the knock on affects of deleting the user from the site collection?  For example, what will happen where the user has created a document (i.e. what will be displayed next to the user name).  If the user is deleted, are all of those items going to be lost? or will they fall back to some default value?

    Thanks for the input.  All help is gratefully appreciated!
    Tuesday, April 07, 2009 4:26 PM
  • Documents or list items added by the user will remain, also the modified or created by field shows the user name, but when click on the user name link, you get user not found message.
    -Sridhar http://sridharu.blogspot.com
    Tuesday, April 07, 2009 5:43 PM