How To Set The User As Owner For All The Groups In A SharePoint Online Site Using CSOM

In this blog, you will see how to set the user as an owner for all the groups in a SharePoint Online site, using CSOM. Please refer my previous article Connect To SharePoint 2013 Online Using CSOM With Console Application

Code Snippet

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Net;  
  5. using System.Security;  
  6. using System.Text;  
  7. using System.Threading.Tasks;  
  8. using Microsoft.SharePoint.Client;  
  9.   
  10. namespace UpdateGroupOwners  
  11. {  
  12.     class Program  
  13.     {  
  14.         static void Main(string[] args)  
  15.         {  
  16.             string userName = "[email protected]";  
  17.             string siteURL = "https://c986.sharepoint.com/sites/Vijai";  
  18.             string user = "[email protected]";  
  19.             Console.WriteLine("Enter your password.");  
  20.             SecureString password = GetPassword();  
  21.   
  22.             // ClienContext - Get the context for the SharePoint Online Site               
  23.             using (var clientContext = new ClientContext(siteURL))  
  24.             {  
  25.                 // SharePoint Online Credentials    
  26.                 clientContext.Credentials = new SharePointOnlineCredentials(userName, password);  
  27.   
  28.                 // Get the SharePoint web    
  29.                 Web web = clientContext.Web;  
  30.   
  31.                 // Get all the site groups  
  32.                 GroupCollection groupColl = web.SiteGroups;  
  33.   
  34.                 // Ensure the user   
  35.                 User ownerUser = web.EnsureUser(user);  
  36.   
  37.                 // Load the site group properties    
  38.                 clientContext.Load(groupColl);  
  39.   
  40.                 // Execute the query to the server.    
  41.                 clientContext.ExecuteQuery();  
  42.   
  43.                 // Loop through all the site groups  
  44.                 foreach (Group group in groupColl)  
  45.                 {  
  46.                     // Display the group title  
  47.                     Console.WriteLine("GroupName: " + group.Title + "-- GroupOwnerTitle: " + group.OwnerTitle);  
  48.   
  49.                     // Update the owner  
  50.                     group.Owner = ownerUser;  
  51.                     group.Update();  
  52.                     clientContext.Load(group);  
  53.   
  54.                     // Execute the query to the server.   
  55.                     clientContext.ExecuteQuery();  
  56.                     Console.WriteLine("UpdatedOwnerTitle: " + group.OwnerTitle);  
  57.                 }  
  58.   
  59.                 Console.ReadLine();  
  60.             }  
  61.         }  
  62.   
  63.         private static SecureString GetPassword()  
  64.         {  
  65.             ConsoleKeyInfo info;  
  66.             //Get the user's password as a SecureString    
  67.             SecureString securePassword = new SecureString();  
  68.             do  
  69.             {  
  70.                 info = Console.ReadKey(true);  
  71.                 if (info.Key != ConsoleKey.Enter)  
  72.                 {  
  73.                     securePassword.AppendChar(info.KeyChar);  
  74.                 }  
  75.             }  
  76.             while (info.Key != ConsoleKey.Enter);  
  77.             return securePassword;  
  78.         }  
  79.     }  
  80. }