Microsoft 365  

Manage Microsoft 365 Users via SPFx and Graph API

Managing user profiles is a critical task for Microsoft 365 administrators. Often, admins or developers need to update user details—such as job title, department, phone number, or display name—programmatically rather than manually through the Microsoft 365 admin center.

With SharePoint Framework (SPFx) and Microsoft Graph API, you can update user properties directly from custom web parts or extensions, enabling automation, consistency, and streamlined profile management.

Why Update User Properties via Graph API in SPFx?

Using the Microsoft Graph API in SPFx provides several benefits:

  • Automation: Update multiple users at once without manual effort.

  • Integration: Embed profile management directly into SharePoint web parts or extensions.

  • Security: Use delegated or application permissions to safely manage user data.

This ensures that user information remains consistent across SharePoint, Teams, Outlook, and other Microsoft 365 services.

Prerequisites

Before you begin, make sure your SPFx solution is set up properly:

1. SPFx Version

Use SPFx v1.6+ to access the MSGraphClientFactory.

2. Graph API Permissions

In package-solution.json, request the following permission:

"webApiPermissionRequests": [
  {
    "resource": "Microsoft Graph",
    "scope": "User.ReadWrite.All"
  }
]

⚠️ Admin consent is required for these permissions.

3. User Identification

You need the User Object ID or user principal name (UPN/email) of the target user.

Microsoft Graph API Endpoint

To update a user property, use the PATCH method on the following endpoint:

PATCH https://graph.microsoft.com/v1.0/users/{user-id}

Request Body Example:

{
  "jobTitle": "Senior Developer",
  "department": "Engineering",
  "mobilePhone": "+1 555-123-4567",
  "officeLocation": "Building 3, Room 210"
}

âś… This endpoint supports updating most standard Azure AD user properties.

Reference: Microsoft Graph User API

SPFx Code Example

Here’s how you can update user properties in SPFx using MSGraphClientFactory:

import { MSGraphClientV3 } from '@microsoft/sp-http';

public async updateUserProperties(userId: string, properties: any): Promise<void> {
  try {
    const client = await this.context.msGraphClientFactory.getClient("3");

    await client.api(`/users/${userId}`).patch(properties);

    console.log(`âś… User ${userId} properties successfully updated`);
  } catch (error) {
    console.error("❌ Error updating user properties:", error);
  }
}

Example Usage

await this.updateUserProperties(
  "1111-2222-3333-4444",
  {
    jobTitle: "Project Manager",
    department: "Marketing",
    mobilePhone: "+1 555-987-6543",
    officeLocation: "Building 5, Room 101"
  }
);

console.log("âś… Alice Johnson's profile updated successfully");

Best Practices

  1. Validate Property Values: Ensure values conform to Azure AD standards (e.g., phone number format).

  2. Handle Errors Gracefully:

    • 403 Forbidden: Missing permissions.

    • 404 Not Found: Invalid user ID.

    • 400 Bad Request: Invalid property values.

  3. Minimize Requests: Combine all property updates in a single PATCH request whenever possible.

  4. Audit Changes: Log updates or notify admins to maintain compliance.

Wrapping Up

Updating Microsoft 365 user profiles using SPFx and Microsoft Graph API enables secure, automated, and consistent management of user information. By leveraging MSGraphClientFactory and the /users/{user-id} PATCH endpoint, developers can:

  • Streamline HR workflows

  • Simplify onboarding and offboarding processes

  • Maintain up-to-date user profiles across Microsoft 365 services

This approach can also be extended to bulk updates, dynamic forms, or automated profile synchronization, making it a key strategy for full lifecycle management of user profiles.