Marcus Lagerstedt
Marcus Lagerstedt

Reputation: 448

Create a Group in Microsoft Graph API with a Owner

I have a Office 365 Group that I would like to add through the Microsoft Graph API. Based on the API documentation, I believe I need

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
}

But when I try to add

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

I get an error (The id is really an id that exists in the office so I don't want to put it here).

They work if I first run the create group and then the add owner but not together. Why?

Would like to make it as easy for me as possible. I'm putting postman in the tags just because that's the tools I'm currently using

Upvotes: 4

Views: 8364

Answers (2)

alex
alex

Reputation: 335

Dan, thank you for this solution! Based on it I created solution which works also with Graph API. The trick is to use the following class which inherits Group from Graph client lib:

public class GroupExtended : Group
{
    [JsonProperty("[email protected]", NullValueHandling = NullValueHandling.Ignore)]
    public string[] OwnersODataBind { get; set; }
    [JsonProperty("[email protected]", NullValueHandling = NullValueHandling.Ignore)]
    public string[] MembersODataBind { get; set; }
}

and then add it like that:

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

Whole solution is described here: http://sadomovalex.blogspot.com/2018/11/create-azure-ad-groups-with-initial.html.

Upvotes: 6

Dan Kershaw - MSFT
Dan Kershaw - MSFT

Reputation: 5838

Actually this is supported and possible today using OData bind syntax (i.e. your syntax is incorrect). NOTE: This is our bad, not your, because we haven't documented this supported behavior. I'll file a bug on our side to document this.

In the meantime, try adding this to your request (it worked for me), and let us know if this works for you:

"[email protected]": ["https://graph.microsoft.com/v1.0/users/{id}"]

In fact, in the same request you can also bind members as part of the request:

"[email protected]": [ "https://graph.microsoft.com/v1.0/users/{id1}" ], "[email protected]": [ "https://graph.microsoft.com/v1.0/users/{id1}", "https://graph.microsoft.com/v1.0/users/{id2}" ]

Not sure what the limits are on the number of items you can put in the binding collection, but I'm sure there is one. I'll see if one of the devs can comment on this.

Hope this helps,

Upvotes: 8

Related Questions