HomeServicesWorkAboutBlogContact Let's Talk
BlogSPFx Development
SPFx Development

Microsoft Graph API in SPFx: Calling the Graph Without a Backend

Using MSGraphClientV3 in Your Web Part

With permissions approved, using the Graph API in your web part is straightforward. The MSGraphClientV3 wraps the official @microsoft/microsoft-graph-client SDK with automatic token injection. You never write a single line of token acquisition code.

src/webparts/myWebPart/MyWebPart.ts — MSGraphClientV3 Usage
import { MSGraphClientV3 } from '@microsoft/sp-http';
import { User } from '@microsoft/microsoft-graph-types';

private async fetchUserAndCalendar(): Promise<void> {
  const client: MSGraphClientV3 = await this.context.msGraphClientFactory
    .getClient('3');

  // Fetch current user profile
  const me: User = await client
    .api('/me')
    .select('id,displayName,mail,jobTitle,department')
    .get();

  // Fetch today's calendar events
  const now = new Date().toISOString();
  const endOfDay = new Date();
  endOfDay.setHours(23, 59, 59);

  const events = await client
    .api('/me/calendarView')
    .query({
      startDateTime: now,
      endDateTime: endOfDay.toISOString(),
    })
    .select('subject,start,end,location')
    .orderby('start/dateTime')
    .top(5)
    .get();

  // Update component state...
}

Calling Users, Calendar, and Mail APIs

The three most commonly used Graph endpoints in SPFx web parts are the user profile, calendar, and mail APIs. Each serves different intranet use cases and requires different permission scopes.

Handling Delegated vs. Application Permissions

SPFx web parts always use delegated permissions — they call the Graph as the logged-in user, with the user's own access rights. This is the correct security posture for client-side code: a user can only see data they're already authorised to see. Application permissions (which let a service access data for any user) are not available to SPFx web parts and require a backend service.

This distinction has practical implications. A web part with User.ReadBasic.All (delegated) can read profile information for all users in the directory — but only if the logged-in user has permission to view that information, which is true for most corporate directories by default. However, if you need to read another user's calendar or email, you typically cannot do this with delegated permissions unless the target user has explicitly shared their calendar. Don't try to use SPFx for admin-level data access scenarios — build a proper Azure Function or Logic App with managed identity for those cases.

Caching Graph Responses for Performance

Graph API calls add latency to your web part's render time. User profile data, org chart information, and configuration data don't change frequently — caching them significantly improves perceived performance without sacrificing data freshness.

Simple sessionStorage Cache for Graph Responses
async function getCachedGraphData<T>(
  cacheKey: string,
  fetcher: () => Promise<T>,
  ttlMinutes: number = 15
): Promise<T> {
  const cached = sessionStorage.getItem(cacheKey);
  if (cached) {
    const { data, expiry } = JSON.parse(cached);
    if (Date.now() < expiry) return data as T;
  }
  const data = await fetcher();
  sessionStorage.setItem(cacheKey, JSON.stringify({
    data,
    expiry: Date.now() + ttlMinutes * 60_000
  }));
  return data;
}

Key Takeaways

MSGraphClientV3 gives SPFx web parts authenticated Graph access with zero backend infrastructure — it's one of SPFx's biggest competitive advantages.

Declare permissions in package-solution.json and approve them in the SharePoint Admin Centre — permissions are tenant-wide, not per-deployment.

SPFx always uses delegated permissions — the web part runs as the logged-in user, not as an admin service. For app-only scenarios, you need a backend service.

Cache Graph responses in sessionStorage for data that changes infrequently — user profiles, org chart data, and configuration are good candidates.

AT

Akshara Technologies Team

Microsoft 365 Development Experts

The Akshara Technologies engineering team specialises in SPFx web parts with Microsoft Graph integration, SharePoint modern intranets, and enterprise Microsoft 365 development for clients across India, UAE, USA, and Australia.

Related Articles

Need Graph API integration in your SharePoint solution?

We build SPFx web parts that surface Microsoft 365 data — calendars, people, files, and Teams activity — beautifully inside SharePoint pages.

Start a Conversation View SPFx Services