How To Remove Bulk Users From The SharePoint Site

In this blog, we will learn about how to remove bulk users from the entire site or from a certain SharePoint group , using CSOM.

Here, we are going to write a code to remove the bulk users from the various sites, which are based on the entries made on a certain SharePoint list. Find the process given below for what we need to do in order to achieve it.

  • Create a SharePoint List named "MainList".
  • Add the columns named - siteURL(single line of text), usertobeRemoved(single line of text), Output(single line of text) in the MainList
  • Add a view in the list with the columns given above in order to retrieve only those columns from the list and name it as MainView.

Column Details

  • siteURL(to be filled)- Site from where the user needs to be removed.
  • usertobeRemoved(to be filled)- User name, who needs to be removed from the site(siteURL). 
  • Output(filled automatically)- Comments once the removal operation has been completed. 

Once the steps given above have been done, create a new console app and add the code given below in order to make it run, as expected. 

Console Application 
  1. namespace ConsoleApplication1  
  2. {  
  3.     class Program  
  4.     {  
  5.         static void Main(string[] args)  
  6.         {  
  7.             string site = string.Empty;  
  8.             string usertobeRemoved = string.Empty;  
  9.             ClientContext context = new ClientContext("Site 1"); //Main Site  
  10.             Web web = context.Web;  
  11.             List oList = web.Lists.GetByTitle("MainList");  
  12.             View oView = oList.Views.GetByTitle("MainView");  
  13.             context.Load(oView);  
  14.             context.ExecuteQuery();  
  15.             CamlQuery camlQuery = new CamlQuery();  
  16.             camlQuery.ViewXml = oView.ViewQuery;  
  17.             ListItemCollection collListItem = oList.GetItems(camlQuery);  
  18.             context.Load(collListItem);  
  19.             
  20.             context.ExecuteQuery();  
  21.               
  22.             foreach (ListItem oListItem in collListItem)  
  23.             {  
  24.                 context.Load(oListItem);  
  25.                 site = Convert.ToString(oListItem["siteURL"]); //retrieves the site url from the list  
  26.                 usertobeRemoved = Convert.ToString(oListItem["usertobeRemoved"]); //retrieves the user details from the list  
  27.                 ClientContext context1 = new ClientContext(site);  
  28.                 User user = context1.Web.EnsureUser(usertobeRemoved); //ensures the user based on the username provided  
  29.                     try  
  30.                     {  
  31.                         context1.Web.SiteUsers.Remove(user); //removes the user  
  32.                         context1.ExecuteQuery();  
  33.                         oListItem["Output"] = "Removed successfully"//updates in the column "Output" as "Removed Successfully" if the user has been removed  
  34.                         oListItem.Update();  
  35.                         context.ExecuteQuery();  
  36.                     }  
  37.                     catch (Exception)  
  38.                     {  
  39.                         oListItem["Output"] = "Removed Failure"//updates in the column "Output" as "Removed Failure" if there is an exception/issue  
  40.                         oListItem.Update();  
  41.                         context.ExecuteQuery();  
  42.                     }  
  43.                 }  
  44.   
  45.   
  46.         }  
  47.     }  
  48. }   
Once the console app has been created, fill in the usertobeRemoved, siteURL in the Mainlist. Run the console app created and for verification purposes, the Output column will get updated, so that we can check whether the user has been removed or not from the site.