如何通过 CSOM 获取组所有者

How to get group owner by CSOM

我正在使用 Sharepoint Online 和 CSOM,我需要获得一个 特定群体。

我尝试从 ClientContext 加载数据,但出现异常消息 "unknown error"。

这是我试过的代码。

MC.ClientContext context = new SPClient.ClientContext("sit url");
MC.Group group = context.Web.SiteGroups.GetById("group id");
context.Load(group, x => x.Owner);
await context.ExecuteQueryAsync();

获取我需要的信息的正确方法是什么?

您可以使用 GroupCollection.GetByName or GetById 检索现有群组,然后检索群组所有者。

using (var context = new ClientContext("sit url"))
{
    context.Credentials = credentials;

    var groupOwner = context.Web.SiteGroups.GetByName("GroupName"); 
    ctx.ExecuteQuery();     
}

我用一种很奇怪的方法解决了这个问题。 代码是这样的:

ClientContext context = new SPClient.ClientContext("sit url");
Group group = context.Web.SiteGroups.GetById("group id");
context.Load(group, x => x.Owner.PrincipalType,  x => x.Owner.LoginName);
await context.ExecuteQueryAsync();
context.Load(group.Owner);
await context.ExecuteQueryAsync();

我需要调用两次ExecuteQuery 方法来获取所有者的对象。 我不知道为什么。但它有效。