Add a member to a security or Microsoft 365 group through the members navigation property.
The following table shows the types of members that can be added to either security groups or Microsoft 365 groups.
Object type
Member of security group
Member of Microsoft 365 group
User
Security group
Microsoft 365 group
Device
Service principal
Organizational contact
Permissions
The following table shows the least privileged permission that's required by each resource type when calling this API. To learn more, including how to choose permissions, see Permissions.
If successful, this method returns a 204 No Content response code. It does not return anything in the response body. This method returns a 400 Bad Request response code when the object is already a member of the group. This method returns a 404 Not Found response code when the object being added doesn't exist.
POST https://graph.microsoft.com/v1.0/groups/{group-id}/members/$ref
Content-type: application/json
{
"@odata.id": "https://graph.microsoft.com/v1.0/directoryObjects/{id}"
}
var graphClient = new GraphServiceClient(requestAdapter);
var requestBody = new Microsoft.Graph.Models.ReferenceCreate
{
OdataId = "https://graph.microsoft.com/v1.0/directoryObjects/{id}",
};
await graphClient.Groups["{group-id}"].Members.Ref.PostAsync(requestBody);
<?php
// THIS SNIPPET IS A PREVIEW FOR THE KIOTA BASED SDK. NON-PRODUCTION USE ONLY
$graphServiceClient = new GraphServiceClient($requestAdapter);
$requestBody = new ReferenceCreate();
$requestBody->set@odataid('https://graph.microsoft.com/v1.0/directoryObjects/{id}');
$graphServiceClient->groupsById('group-id')->members()->ref()->post($requestBody);
In the request body, supply a JSON representation of the id of the directoryObject, user, or group object you want to add.
Response
The following is an example of the response.
HTTP/1.1 204 No Content
Example 2: Add multiple members to a group in a single request
This example shows how to add multiple members to a group with OData bind support in a PATCH operation. Note that up to 20 members can be added in a single request. The POST operation is not supported. If an error condition exists in the request body, no members are added and the appropriate response code is returned.
var graphClient = new GraphServiceClient(requestAdapter);
var requestBody = new Group
{
AdditionalData = new Dictionary<string, object>
{
{
"members@odata.bind" , new List<string>
{
"https://graph.microsoft.com/v1.0/directoryObjects/{id}",
"https://graph.microsoft.com/v1.0/directoryObjects/{id}",
"https://graph.microsoft.com/v1.0/directoryObjects/{id}",
}
},
},
};
var result = await graphClient.Groups["{group-id}"].PatchAsync(requestBody);
GraphServiceClient graphClient = GraphServiceClient.builder().authenticationProvider( authProvider ).buildClient();
Group group = new Group();
group.additionalDataManager().put("members@odata.bind", new JsonPrimitive("[ \"https://graph.microsoft.com/v1.0/directoryObjects/{id}\", \"https://graph.microsoft.com/v1.0/directoryObjects/{id}\", \"https://graph.microsoft.com/v1.0/directoryObjects/{id}\"]"));
graphClient.groups("{group-id}")
.buildRequest()
.patch(group);
<?php
// THIS SNIPPET IS A PREVIEW FOR THE KIOTA BASED SDK. NON-PRODUCTION USE ONLY
$graphServiceClient = new GraphServiceClient($requestAdapter);
$requestBody = new Group();
$additionalData = [
'members@odata.bind' => ['https://graph.microsoft.com/v1.0/directoryObjects/{id}', 'https://graph.microsoft.com/v1.0/directoryObjects/{id}', 'https://graph.microsoft.com/v1.0/directoryObjects/{id}', ],
];
$requestBody->setAdditionalData($additionalData);
$requestResult = $graphServiceClient->groupsById('group-id')->patch($requestBody);