How to add a SharePoint group as Group Owner with Client Object Model

sharepoint

If you want to create a group and add group owner and default user you can use the following codes:

string siteUrl = "https://server/sites/sitename";
ClientContext clientContext = new ClientContext(siteUrl);
Web web = clientContext.Web;

GroupCreationInformation groupCreationInfo = new GroupCreationInformation();
groupCreationInfo.Title = "Custom Group";
groupCreationInfo.Description = "description ...";

User owner = web.EnsureUser(@"domain\username1");    
User member = web.EnsureUser(@"domain\username2");

Group group = web.SiteGroups.Add(groupCreationInfo);    
group.Owner = owner;             
group.Users.AddUser(member);     
group.Update(); 

clientContext.ExecuteQuery();

My question is: I know how to add a user as Group Owner but if I want to add a SharePoint group "Tech Support" as the group owner what the code should be?

Best Answer

Use GroupCollection.GetByName or GroupCollection.GetById method to retrieve an existing group from site and then set Group.Owner property to its value, for example:

using (var ctx = new ClientContext(webUri))
{
    ctx.Credentials = credentials;

    var groupCreationInfo = new GroupCreationInformation
    {
         Title = groupName,
         Description = groupDesc
    };

    var groupOwner = ctx.Web.SiteGroups.GetByName("Tech Support"); //get an existing group

    var group = ctx.Web.SiteGroups.Add(groupCreationInfo);
    group.Owner = groupOwner;
    group.Update();
    ctx.ExecuteQuery();     
}
Related Topic