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

5
如果您想创建一个组并添加组所有者和默认用户,可以使用以下代码:
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个回答

6

使用GroupCollection.GetByNameGroupCollection.GetById方法从站点中检索已存在的组,然后将Group.Owner属性设置为其值,例如:

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

谢谢,Vadim。我试过GetById(),它有效。我尝试了GetByName(),但它没用,我猜这是因为我在VS 2010中尝试了它。我稍后会在VS 2013中尝试一下。但我认为这就是解决方案。 - allan8964
没错,GroupCollection.GetByName是在SharePoint 2013 CSOM API中引入的。 - Vadim Gremyachev
我尝试投票,但需要注册,找不到链接在哪里。 - allan8964

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接