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. namespace GetUsersFromGroup
  10. {
  11. class Program
  12. {
  13. static void Main(string[] args)
  14. {
  15. string userName = "[email protected]";
  16. string password = "@@#$%^&";
  17. SecureString pwd = new SecureString();
  18. foreach (char c in password)
  19. {
  20. pwd.AppendChar(c);
  21. }
  22. // Read the text file where site URLs are available
  23. string[] URLs = System.IO.File.ReadAllLines(@"C:\Users\anavi\Documents\Visual Studio 2017\Projects\GetUsersFromGroup\GetUsersFromGroup\Sites.txt");
  24. // Loop through each site URL
  25. foreach (string siteURL in URLs)
  26. {
  27. Console.WriteLine(siteURL);
  28. GetOwners(userName, pwd, siteURL);
  29. }
  30. Console.ReadLine();
  31. }
  32. private static void GetOwners(string userName, SecureString pwd, string siteURL)
  33. {
  34. // Get the SharePoint client context
  35. using (var ctx = new ClientContext(siteURL))
  36. {
  37. // Set the credentials
  38. ctx.Credentials = new SharePointOnlineCredentials(userName, pwd);
  39. // Get all the users from SharePoint default owners group
  40. UserCollection owners = ctx.Web.AssociatedOwnerGroup.Users;
  41. // Load the user collection
  42. ctx.Load(owners);
  43. // Execute the query
  44. ctx.ExecuteQuery();
  45. // Check if the owners are not null
  46. if (owners != null)
  47. {
  48. // Loop through all the owners
  49. foreach (var owner in owners)
  50. {
  51. // Check if the owner email is not empty
  52. // O365 group added to owners groups will be displayed for Modern sites
  53. // If you want to retrieve only the users from default owners group then check if principal type is owner
  54. if (owner.PrincipalType.ToString() == "User" && !String.IsNullOrEmpty(owner.Email))
  55. {
  56. // Display the owner email ID
  57. Console.WriteLine(owner.Email);
  58. }
  59. }
  60. }
  61. }
  62. }
  63. }
  64. }
Thus in this blog, you saw how to get all the users from SharePoint Online site default owners group using CSOM in Console Application.