Managing group membership is a common task in Microsoft 365. While adding users to groups helps onboard employees or students, sometimes you need to remove users—for example, when someone leaves a team or a course ends.
With SharePoint Framework (SPFx) and Microsoft Graph API, removing users from Microsoft 365 Groups is straightforward, allowing you to automate and streamline group management.
Why Use Graph API in SPFx for Removing Users?
Microsoft Graph provides a unified, secure API to manage Microsoft 365 services. Using SPFx’s MSGraphClientFactory
, you can:
Remove users from groups in real-time.
Automate workflows for onboarding/offboarding.
Integrate group management directly into SharePoint web parts or admin dashboards.
This avoids manual administration through the Microsoft 365 admin center.
Prerequisites
Before removing users, ensure your SPFx project has the following setup:
SPFx Version
Use SPFx v1.6+ for full Graph support.
Graph API Permissions
In package-solution.json
, request:
"webApiPermissionRequests": [
{
"resource": "Microsoft Graph",
"scope": "Group.ReadWrite.All"
},
{
"resource": "Microsoft Graph",
"scope": "User.Read.All"
}
]
Admin consent is required.
Group and User IDs
You need the Group ID and the User’s Object ID from Azure AD.
Graph API Endpoint
To remove a user from a group, Microsoft Graph uses the DELETE method:
DELETE https://graph.microsoft.com/v1.0/groups/{group-id}/members/{user-id}/$ref
No request body is needed—just the group and user IDs.
SPFx Code Example
Here’s how you can remove a user from a Microsoft 365 Group in SPFx:
public async removeUserFromGroup(groupId: string, userId: string): Promise<void> {
try {
const client = await this.context.msGraphClientFactory.getClient("3");
await client.api(`/groups/${groupId}/members/${userId}/$ref`).delete();
console.log(`âś… User ${userId} successfully removed from group ${groupId}`);
} catch (error) {
console.error("❌ Error removing user from group:", error);
}
}
Dummy Data Example
Suppose you want to remove Alice Johnson (userId: 1111-2222-3333-4444
) from the Marketing Team group (groupId: abcd-1234-efgh-5678
):
await this.removeUserFromGroup(
"abcd-1234-efgh-5678", // Marketing Team Group ID
"1111-2222-3333-4444" // Alice Johnson's User Object ID
);
console.log("âś… User Alice Johnson removed from Marketing Team group");
Best Practices
Verify Membership Before Removal
Avoid errors by checking if the user is in the group:
const members = await client.api(`/groups/${groupId}/members`).get();
Handle Errors Gracefully
403 Forbidden
: Missing permissions.
404 Not Found
: Invalid group or user ID.
400 Bad Request
: Attempted to remove a non-member.
Secure Your IDs
Do not hardcode sensitive IDs—fetch user and group IDs dynamically from Graph queries.
User Feedback
Notify admins or users via the UI when a member is removed.
Wrapping Up
Removing users from Microsoft 365 Groups in SPFx is simple, fast, and scalable. By leveraging Microsoft Graph, you can automate offboarding processes, manage team memberships efficiently, and integrate membership management directly into your SharePoint solutions.
With a combination of MSGraphClientFactory
and Graph’s DELETE /groups/{group-id}/members/{user-id}/$ref
endpoint, your SPFx web parts can fully manage the group membership lifecycle.