创建群组时添加群组成员(msgraph-sdk-java)

时间:2019-02-16 22:14:34

标签: java azure-active-directory microsoft-graph microsoft-graph-sdks

我想在组创建时将成员添加到组。我该如何实现?

请求:

POST https://graph.microsoft.com/v1.0/groups
Content-Type: application/json

{
  "displayName": "displayName",
  "mailEnabled": true,
  "mailNickname": "mailNickname",
  "securityEnabled": false,
  "groupTypes": [
    "Unified"
  ],
  "members@odata.bind": [
    "https://graph.microsoft.com/v1.0/users/{userId}"
  ]
}

和代码:

Group group = new Group();

group.displayName = "displayName";  
group.mailEnabled = true;
group.mailNickname = "mailNickname";
group.securityEnabled = false;    
List<String> groupTypes = new ArrayList<>();
groupTypes.add("Unified");
group.groupTypes=groupTypes;
String userId = "userId";   

//HERE add members to group
//group.members=???

serviceClient.groups().buildRequest().post(group);

我应该如何在哪里写“ members@odata.bind”部分?

1 个答案:

答案 0 :(得分:0)

用于指定成员的一个选项是构造自定义查询

JsonObject groupPayload = new JsonObject();
groupPayload.addProperty("displayName","<value>");
groupPayload.addProperty("mailNickname","<value>");
groupPayload.addProperty("mailEnabled",true);
groupPayload.addProperty("securityEnabled",false);
JsonArray groupTypes = new JsonArray();
groupTypes.add("Unified");
groupPayload.add("groupTypes",groupTypes);

//specify members
JsonArray members = new JsonArray();
members.add(String.format("https://graph.microsoft.com/v1.0/users/%s",userId));
groupPayload.add("members@odata.bind",members);

JsonObject response  = graphClient.customRequest("/groups").buildRequest().post(groupPayload).getAsJsonObject();