Skip to content
@aploon

Integrating Microsoft Graph into a Business Application

  • #microsoft
  • #microsoftgraph
  • #api
  • #dotnet
  • #integration

Business applications frequently need to interact with Microsoft 365 resources such as SharePoint sites, document libraries, Microsoft Teams, and user information.

Microsoft Graph provides a unified API for accessing supported Microsoft cloud services through a consistent interface.

Instead of building separate integrations for every Microsoft 365 service, an application can use Microsoft Graph endpoints to perform operations supported by the permissions granted to it.

However, a successful integration requires more than sending HTTP requests. Authentication, authorization, resource identifiers, error handling, and operational reliability must all be considered.

1. Understand the integration architecture

A typical integration contains four components:

  1. The business application, which implements the business workflow.
  2. An authentication mechanism, which obtains an access token.
  3. Microsoft Graph, which exposes supported Microsoft 365 operations.
  4. The target resource, such as a SharePoint site or document library.

For example, a quality-management application might need to create a folder for a nonconformity record and store related documents in SharePoint.

The business application remains responsible for deciding when the folder should be created and what it should contain. Microsoft Graph provides the API operations needed to interact with the SharePoint resource.

This separation keeps business rules distinct from external service integration.

2. Configure authentication and permissions

Before calling Microsoft Graph, register the application in Microsoft Entra ID and configure an appropriate authentication flow.

Two common models are:

  • Delegated access: The application operates on behalf of a signed-in user.
  • Application access: The application operates under its own identity, without a signed-in user.

The correct model depends on the workflow. A background process that creates folders automatically may need application permissions, whereas an interactive user-facing feature may be better suited to delegated access.

Grant only the permissions required by the operations the application performs. Where supported, use resource-specific or more narrowly scoped access rather than broad permissions.

Application permissions may require administrator consent. Access tokens should be obtained through a supported authentication library and should not be stored in source code or exposed to the browser unnecessarily.

3. Use the Microsoft Graph SDK

In a .NET application, the Microsoft Graph SDK can provide a typed client for supported API operations.

The following example illustrates the shape of an SDK call for retrieving a SharePoint site by its hostname and path. It assumes that graphClient has already been configured with a valid authentication provider and appropriate permissions.

var site = await graphClient
    .Sites["contoso.sharepoint.com:/sites/Quality"]
    .GetAsync();
 
if (site is null)
{
    throw new InvalidOperationException(
        "The requested SharePoint site was not returned."
    );
}

This is an illustrative example; the hostname, site path, SDK version, and authentication configuration must match the actual environment.

A typed SDK can reduce repetitive request-building code, but developers should still understand the underlying REST endpoint, its permission requirements, and its response behavior.

4. Work with SharePoint document libraries

Consider a business application that organizes documents by record.

A folder structure might look like this:

Nonconformity_Documents/
โ””โ”€โ”€ NC-1042/
    โ”œโ”€โ”€ Definition/
    โ”œโ”€โ”€ Evidence/
    โ””โ”€โ”€ CorrectiveActions/

This is an example of a possible document organization strategy, not a required Microsoft Graph structure.

A typical implementation would:

  1. Resolve the target SharePoint site.
  2. Identify the appropriate document library or drive.
  3. Create the record's folder when necessary.
  4. Create or resolve related folders.
  5. Upload or generate the required documents.
  6. Save relevant resource identifiers in the business application's database.

The application should define what happens when a folder already exists. Blindly repeating folder-creation requests can cause conflicts, so an implementation may need to resolve the existing resource and continue safely.

Persisting stable identifiers can also be more reliable than repeatedly looking up resources by display name.

5. Handle errors and transient failures

External API calls can fail for several reasons:

  • The token is missing required permissions.
  • A resource does not exist or is inaccessible.
  • The request is invalid.
  • Microsoft Graph applies throttling.
  • A service experiences a temporary failure.
  • The network request times out.

Production integrations should distinguish between errors that require configuration changes and errors that may succeed after a retry.

In particular, HTTP 429 Too Many Requests responses can include a Retry-After value. A client should respect the service's retry guidance rather than repeatedly sending requests at full speed.

Use bounded retries with appropriate backoff for transient failures. Avoid retrying every error indiscriminately, especially when the operation may have already created or modified a resource.

Log useful diagnostic context, but never write access tokens, secrets, or sensitive document contents into application logs.

6. Make integration operations resilient

A business workflow may involve several steps: create a database record, create a SharePoint folder, generate a document, and update the record with the document location.

These operations generally do not form one atomic transaction across the database and Microsoft Graph.

If the third step fails, the application may be left with a database record and a partially created SharePoint structure.

A robust design can use explicit workflow states, retryable operations, and reconciliation jobs.

For example:

Pending
  โ†’ SharePointFolderCreated
  โ†’ DocumentsGenerated
  โ†’ Completed
 
Any recoverable failure
  โ†’ RetryPending

This state model is illustrative. Actual states should reflect the business process.

Where possible, make each step idempotent: repeating it should not create duplicate folders, duplicate records, or unintended side effects.

For more complex workflows, persist the operation status and resume from the last confirmed step instead of restarting everything blindly.

7. Separate API integration from business logic

Avoid spreading Microsoft Graph calls throughout controllers and unrelated business services.

A dedicated integration service can centralize resource resolution, API operations, error translation, and logging.

For example, a business service might request that a document workspace be created without needing to know the details of every Graph endpoint.

This makes the code easier to test and allows the integration to evolve independently of the business workflow.

It also provides a natural boundary for mocking external API behavior during automated tests.

8. Test the integration realistically

Unit tests can verify how the application reacts to successful responses, missing resources, permission failures, and transient errors.

Integration tests can validate authentication and actual Microsoft Graph operations in a controlled Microsoft 365 environment.

Tests should cover both success and failure paths, including the case where a request succeeds remotely but the application fails before recording the result locally.

Because external resources and permissions affect behavior, configuration should be explicit and test environments should use appropriately limited access.

Microsoft Graph enables business applications to work with supported Microsoft 365 resources through a unified API.

A maintainable integration combines correct authentication, least-privilege permissions, a clear service boundary, deliberate error handling, and recovery from partial failures.

The objective is not simply to call an endpoint successfully. It is to make the external integration a dependable part of the business application's architecture.