Add Or Remove User From Group Using CSOM

In this article we learn how to get all groups and add or remove user from all that groups using SharePoint CSOM.

Introduction

 
Learn how to build an app to get all groups, and add and remove users using SharePoint CSOM.
 
Step 1 - Create a Project
 
After opening Visual Studio, next, we need to create an ASP.NET MVC project. For doing that, just click File - New - Project.
 
Add Or Remove User From Group Using CSOM
 
After choosing a project, a new dialog will pop up. In that, select Console Application and give your project the location and a name. Then, click the "Ok" button.
 
Add Or Remove User From Group Using CSOM 

Then after the Program.cs class will open and you will see the below blank main class.
  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.Linq;    
  4. using System.Text;    
  5. using System.Threading.Tasks;    
  6.         
  7. namespace MergePDF    
  8. {    
  9.     class Program    
  10.     {    
  11.         static void Main(string[] args)    
  12.         {    
  13.     
  14.         }    
  15.     }    
  16. }    
Step 2 - Install two Nuget Package
  1. Microsoft.SharePoint.Client
  2. Microsoft.SharePointOnline.CSOM
Right click on References in Solution explorer and select "Manage Nuget Package". On selecting it, the popup window will open like below.
 
Add Or Remove User From Group Using CSOM
 
Add the below namespace.
  1. using Microsoft.SharePoint.Client;  
  2. using System;  
  3. using System.IO;  
  4. using System.Security;  
  5. using System.Text;  
Then copy below code and past into your program.cs file.
  1. class Program  
  2.    {  
  3.        public static string _userName = "Test\\KDUser";  
  4.        public static string _password = "Password01";  
  5.        public static string _url = "http://TestLink:2019/";  
  6.   
  7.        static void Main(string[] args)  
  8.        {  
  9.            ClientContext clientContext = GetContext();  
  10.   
  11.            var GroupData = GetAllGroup(clientContext);  
  12.            foreach (var item in GroupData)  
  13.            {  
  14.                Console.WriteLine(item.Title);  
  15.                var userData = item.Users;  
  16.                AddUser(clientContext, item, _userName);  
  17.            }  
  18.            Console.ReadLine();  
  19.        }  
  20.   
  21.   
  22.        public static void AddUser(ClientContext clientContext, Group group, string logonName)  
  23.        {  
  24.            User aoUser = clientContext.Web.EnsureUser(logonName);  
  25.            User oUser = group.Users.AddUser(aoUser);  
  26.            clientContext.ExecuteQuery();  
  27.        }  
  28.   
  29.        public static void RemoveUSer(ClientContext clientContext, Group group, string logonName)  
  30.        {  
  31.            User oUser = clientContext.Web.EnsureUser(logonName);  
  32.   
  33.            clientContext.Load(oUser);  
  34.            clientContext.ExecuteQuery();  
  35.            group.Users.RemoveByLoginName(oUser.LoginName);  
  36.            clientContext.ExecuteQuery();  
  37.        }  
  38.   
  39.   
  40.        public static GroupCollection GetAllGroup(ClientContext clientContext)  
  41.        {  
  42.            GroupCollection list = clientContext.Web.SiteGroups;  
  43.            clientContext.Load(list);  
  44.            clientContext.ExecuteQuery();  
  45.            return list;  
  46.        }  
  47.   
  48.        public static ClientContext GetContext()  
  49.        {  
  50.            SecureString passWord = new SecureString();  
  51.            foreach (char c in _password.ToCharArray())  
  52.                passWord.AppendChar(c);  
  53.   
  54.            ClientContext clientContext = new ClientContext(_url);  
  55.            clientContext.Credentials = new System.Net.NetworkCredential(_userName, passWord);  
  56.            return clientContext;  
  57.        }  
  58.   
  59.    }  
The GetContext() function is used to get the current context.
 
The GetAllGroup(ClientContext clientContext) function is used to get the data of all group in cuurent site.

The AddUser(ClientContext clientContext, Group group, string logonName) function will add a given user to current Group.

The RemoveUSer(ClientContext clientContext, Group group, string logonName) function will remove user from current Group.

Please don't forget to set the _userName , _password and _url.