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

In this blog, you will see how to set the group as an owner for all the groups in a SharePoint Online site, using CSOM. Please refer to 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 ownerGroupName = "Site Owners";  
  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.                 // Get the group name which has to be set as owner to all the groups  
  35.                 Group ownerGroup = web.SiteGroups.GetByName(ownerGroupName);  
  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 = ownerGroup;  
  51.                     group.Update();  
  52.                     clientContext.Load(group);  
  53.   
  54.                     // Execute the query to the server.   
  55.                     clientContext.ExecuteQuery();  
  56.   
  57.                     // Display the updated group owner title  
  58.                     Console.WriteLine("UpdatedOwnerTitle: " + group.OwnerTitle);  
  59.                 }  
  60.                 Console.ReadLine();  
  61.             }  
  62.         }  
  63.   
  64.         private static SecureString GetPassword()  
  65.         {  
  66.             ConsoleKeyInfo info;  
  67.             //Get the user's password as a SecureString    
  68.             SecureString securePassword = new SecureString();  
  69.             do  
  70.             {  
  71.                 info = Console.ReadKey(true);  
  72.                 if (info.Key != ConsoleKey.Enter)  
  73.                 {  
  74.                     securePassword.AppendChar(info.KeyChar);  
  75.                 }  
  76.             }  
  77.             while (info.Key != ConsoleKey.Enter);  
  78.             return securePassword;  
  79.         }  
  80.     }  
  81. }