如何使用客户端对象模型将SharePoint组添加为组所有者?

时间:2014-09-03 21:24:13

标签: sharepoint

如果要创建组并添加组所有者和默认用户,可以使用以下代码:

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();

我的问题是:我知道如何将用户添加为组所有者,但是如果我想将SharePoint组“技术支持”作为组所有者添加代码应该是什么?

1 个答案:

答案 0 :(得分:6)

使用GroupCollection.GetByNameGroupCollection.GetById方法从网站检索现有群组,然后将Group.Owner property设置为其值,例如:

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();     
}