- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Security;
- using System.Text;
- using System.Threading.Tasks;
- using Microsoft.SharePoint.Client;
-
- namespace UpdateGroupOwners
- {
- class Program
- {
- static void Main(string[] args)
- {
- string userName = "[email protected]";
- string siteURL = "https://c986.sharepoint.com/sites/Vijai";
- string ownerGroupName = "Site Owners";
- Console.WriteLine("Enter your password.");
- SecureString password = GetPassword();
-
-
- using (var clientContext = new ClientContext(siteURL))
- {
-
- clientContext.Credentials = new SharePointOnlineCredentials(userName, password);
-
-
- Web web = clientContext.Web;
-
-
- GroupCollection groupColl = web.SiteGroups;
-
-
- Group ownerGroup = web.SiteGroups.GetByName(ownerGroupName);
-
-
- clientContext.Load(groupColl);
-
-
- clientContext.ExecuteQuery();
-
-
- foreach (Group group in groupColl)
- {
-
- Console.WriteLine("GroupName: " + group.Title + "-- GroupOwnerTitle: " + group.OwnerTitle);
-
-
- group.Owner = ownerGroup;
- group.Update();
- clientContext.Load(group);
-
-
- clientContext.ExecuteQuery();
-
-
- Console.WriteLine("UpdatedOwnerTitle: " + group.OwnerTitle);
- }
- Console.ReadLine();
- }
- }
-
- private static SecureString GetPassword()
- {
- ConsoleKeyInfo info;
-
- SecureString securePassword = new SecureString();
- do
- {
- info = Console.ReadKey(true);
- if (info.Key != ConsoleKey.Enter)
- {
- securePassword.AppendChar(info.KeyChar);
- }
- }
- while (info.Key != ConsoleKey.Enter);
- return securePassword;
- }
- }
- }