首页 文章

使用所有者在Microsoft Graph API中创建一个组

提问于
浏览
3

我有一个Office 365组,我想通过Microsoft Graph API添加 . 基于API文档,我相信我需要

POST https://graph.microsoft.com/v1.0/groups
Content-type: application/json
Content-length: 244
{
  "description": "Self help community for library",
  "displayName": "Library Assist",
  "groupTypes": [
    "Unified"
  ],
  "mailEnabled": true,
  "mailNickname": "library",
  "securityEnabled": false
}

但是当我尝试添加时

"owner": [{ "@odata.id": "https://graph.microsoft.com/v1.0/users/{id}"}]

我收到一个错误(该ID实际上是办公室中存在的ID,所以我不想把它放在这里) .

如果我首先运行创建组然后添加所有者但不在一起,它们可以工作 . 为什么?

想尽可能让我这么容易 . 我将邮递员放在标签中只是因为那是我目前正在使用的工具

2 回答

  • 5

    实际上,今天使用OData绑定语法(即您的语法不正确)支持并且可能 . 注意:这是我们的坏,而不是你的,因为我们没有记录这种支持的行为 . 我会在我们这边提交一个错误来记录这个 .

    在此期间,请尝试将此添加到您的请求中(它对我有用),并告诉我们这是否适合您:

    "owners@odata.bind": ["https://graph.microsoft.com/v1.0/users/{id}"]

    实际上,在同一个请求中,您还可以将成员绑定为请求的一部分:

    "owners@odata.bind": [ "https://graph.microsoft.com/v1.0/users/{id1}" ], "members@odata.bind": [ "https://graph.microsoft.com/v1.0/users/{id1}", "https://graph.microsoft.com/v1.0/users/{id2}" ]

    不确定你可以在绑定集合中放置的项目数量有什么限制,但我确信有一个 . 我会看看其中一个开发者是否可以对此发表评论 .

    希望这可以帮助,

  • 0

    丹,谢谢你的解决方案!基于它,我创建了与Graph API一起使用的解决方案 . 诀窍是使用以下类从Graph客户端lib继承Group:

    public class GroupExtended : Group
    {
        [JsonProperty("owners@odata.bind", NullValueHandling = NullValueHandling.Ignore)]
        public string[] OwnersODataBind { get; set; }
        [JsonProperty("members@odata.bind", NullValueHandling = NullValueHandling.Ignore)]
        public string[] MembersODataBind { get; set; }
    }
    

    然后像这样添加它:

    var newGroup = new GroupExtended
    {
        DisplayName = displayName,
        Description = description,
        MailNickname = mailNickname,
        MailEnabled = true,
        SecurityEnabled = false,
        Visibility = isPrivate == true ? "Private" : "Public",
        GroupTypes = new List<string> { "Unified" }
    };
    
    if (owners != null && owners.Length > 0)
    {
        var users = GetUsers(graphClient, owners);
        if (users != null)
        {
            newGroup.OwnersODataBind = users.Select(u => string.Format("https://graph.microsoft.com/v1.0/users/{0}", u.Id)).ToArray();
        }
    }
    
    if (members != null && members.Length > 0)
    {
        var users = GetUsers(graphClient, members);
        if (users != null)
        {
            newGroup.MembersODataBind = users.Select(u => string.Format("https://graph.microsoft.com/v1.0/users/{0}", u.Id)).ToArray();
        }
    }
    
    await graphClient.Groups.Request().AddAsync(newGroup);
    

    整个解决方案在这里描述:http://sadomovalex.blogspot.com/2018/11/create-azure-ad-groups-with-initial.html .

相关问题