2014-09-03 47 views
5

如果你想创建一个组,并添加组所有者和默认的用户可以使用下面的代码:如何使用客户端对象模型将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组“技术支持”作为集团所有者的代码应该是什么?

回答

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

由于瓦迪姆。我试过GetById()它的工作。我试过GetByName()不工作,我想这是因为我在VS 2010中试过了。我将在稍后的VS 2013中尝试。但我认为这是解决方案。 – allan8964 2014-09-04 13:51:11

+0

没错,GroupCollection.GetByName在SharePoint 2013中引入了CSOM API – 2014-09-04 14:29:09

+0

GetByName()在VS 2013中工作。再次感谢! – allan8964 2014-09-04 15:50:46

相关问题