How to get all the users from SharePoint Online sites default owners group using CSOM

In this blog, you will see how to get all the users from SharePoint Online site default owners group using CSOM in Console Application.
 
By default, when a new SharePoint site is provisioned default owners, members and visitors groups are created. I need to get all the users from default owners group. I have to get this information from multiple SharePoint sites for which I have created a text file (input/configuration file) that contains all the SharePoint site URL’s.
 
Steps Involved
 
Open Visual Studio (2017 or higher).
 
Create a console application and name it as GetUsersFromGroup.
 
In the NuGet Package Manager, install Microsoft.SharePointOnline.CSOM package.
 
Add a new text file and name it as Sites.txt. Enter all the site URLs from which owners need to be retrieved.
 
Replace Program.cs code with the following.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.IO;  
  5. using System.Text;  
  6. using System.Security;  
  7. using System.Threading.Tasks;  
  8. using Microsoft.SharePoint.Client;  
  9.   
  10. namespace GetUsersFromGroup  
  11. {  
  12.     class Program  
  13.     {  
  14.         static void Main(string[] args)  
  15.         {  
  16.             string userName = "[email protected]";  
  17.             string password = "@@#$%^&";  
  18.             SecureString pwd = new SecureString();  
  19.             foreach (char c in password)  
  20.             {  
  21.                 pwd.AppendChar(c);  
  22.             }  
  23.   
  24.             // Read the text file where site URLs are available  
  25.             string[] URLs = System.IO.File.ReadAllLines(@"C:\Users\anavi\Documents\Visual Studio 2017\Projects\GetUsersFromGroup\GetUsersFromGroup\Sites.txt");  
  26.             // Loop through each site URL  
  27.             foreach (string siteURL in URLs)  
  28.             {  
  29.                 Console.WriteLine(siteURL);  
  30.                 GetOwners(userName, pwd, siteURL);  
  31.             }  
  32.             Console.ReadLine();  
  33.         }  
  34.   
  35.         private static void GetOwners(string userName, SecureString pwd, string siteURL)  
  36.         {  
  37.             // Get the SharePoint client context  
  38.             using (var ctx = new ClientContext(siteURL))  
  39.             {  
  40.                 // Set the credentials  
  41.                 ctx.Credentials = new SharePointOnlineCredentials(userName, pwd);  
  42.                 // Get all the users from SharePoint default owners group  
  43.                 UserCollection owners = ctx.Web.AssociatedOwnerGroup.Users;  
  44.                 // Load the user collection  
  45.                 ctx.Load(owners);  
  46.                 // Execute the query  
  47.                 ctx.ExecuteQuery();  
  48.   
  49.                 // Check if the owners are not null  
  50.                 if (owners != null)  
  51.                 {  
  52.                     // Loop through all the owners  
  53.                     foreach (var owner in owners)  
  54.                     {  
  55.                         // Check if the owner email is not empty  
  56.                         // O365 group added to owners groups will be displayed for Modern sites  
  57.                         // If you want to retrieve only the users from default owners group then check if principal type is owner  
  58.                         if (owner.PrincipalType.ToString() == "User" && !String.IsNullOrEmpty(owner.Email))  
  59.                         {  
  60.                             // Display the owner email ID  
  61.                             Console.WriteLine(owner.Email);  
  62.                         }  
  63.                     }  
  64.                 }  
  65.             }  
  66.         }  
  67.     }  
  68. }  
Thus in this blog, you saw how to get all the users from SharePoint Online site default owners group using CSOM in Console Application.