Skip to main content

Delete the Staging server Users from the User Information List

When you perform the site backup if you remove the users from the group but still the user details are available in the user information list

Delete the Staging server Users from the User Information List


 public void DeleteStagingUsers(string siteurl, string UserName)
        {
            try
            {
                using (SPSite osite = new SPSite(siteurl))
                {
                    foreach(SPWeb oweb in osite.AllWebs)
                    {
                        SPList oinfolist = oweb.Lists["User Information List"];
                        oweb.SiteUserInfoList.AllowDeletion = true;
                        SPListItemCollection oitemcol = oinfolist.Items;
                        foreach (SPListItem oitm in oitemcol)
                        {
                            string strName = Convert.ToString(oitm["Name"]);
                            if (strName.ToLower().Contains(UserName.ToLower() ))
                            {
                                oweb.AllowUnsafeUpdates = true;
                                int ItemId = Convert.ToInt32(oitm["ID"]);
                                oweb.SiteUsers.RemoveByID(ItemId);
                                oweb.AllowUnsafeUpdates = false;
                            }
                        }
                    }
 
                }
            }
        }

Comments

Popular posts from this blog

How to store the sharepoint List or List Item within the recycle bin of the site while deleting it programmatically ?

Whenever we delete a sharepoint list or list item from the User Interface it get stored within the recycle bin of the site. But when we delete it programmatically it get's deleted permanently. So what we can do to store the List or List Item within the recycle bin while deleting it programmatically ? Generally we use the list.Delete() or item.Delete() method to delete the list or item. Instead of using the Delete() method we can use the Recycle() method i.e list.Recycle() or item.Recycle(). This will store the item within the recycle bin.