How to Store Documents in SharePoint with Microsoft Graph — AI Automation article by Rui Codex

Your business application can store documents in SharePoint by calling the Microsoft Graph driveItem API with a token that holds write permission on the target document library. Small files can go in a single PUT request, while larger files need an upload session. Settle the permission model, folder path, conflict handling and error handling before writing the upload code.

Key takeaways

  • Graph writes to SharePoint through the document library's drive, so you must resolve the site and drive IDs before uploading.
  • The calling identity needs both a suitable Graph permission and write access to the library, or the request fails.
  • Use a simple upload for small files and an upload session for large or interruption-prone transfers.
  • Set the conflict behaviour and metadata deliberately, because silent overwrites and missing required columns are common failure points.
  • Log every upload with its response status and retry count so production problems can be traced.

Where do uploaded documents go in SharePoint when Graph writes them?

Graph does not write to a SharePoint site directly. It writes to a drive, and in SharePoint the document library is exposed as a drive. Your application therefore needs the site ID and the drive ID before it can address a folder or file.

A typical lookup starts with the site, then lists the drives on that site and selects the library by name. Store the resulting IDs in configuration rather than resolving them on every request, because the lookup adds a round trip to every upload. The official overview of how Graph models these resources is at the Microsoft Graph documentation.

Paths matter as much as IDs. A folder path that works in the browser may fail through the API if it contains characters the path encoding does not handle, or if the folder does not exist yet. Create missing folders explicitly and encode each path segment.

Tip: Resolve site and drive IDs once during deployment, then keep them in configuration. Re-resolving them per request is a common source of avoidable throttling.

When should you use a simple upload instead of an upload session?

Use a simple upload for small files that can be sent in one request and retried as a whole. Use an upload session for large files, or for any transfer that must survive a dropped connection. The simple approach is easier to write, but it has to restart from the beginning when it fails.

An upload session creates a temporary upload URL, sends the file in byte ranges, and can resume from the last accepted range. The request flow and its limits are described in the driveItem createUploadSession reference. Check the current size thresholds there before you hard-code any branching logic, because they are documented by Microsoft and can change.

CriterionSimple uploadUpload session
Typical file sizeSmaller filesLarger files
Resume after interruptionNo, restart the requestYes, from the last accepted range
Implementation effortLowModerate
Failure costEntire file resentOnly missing ranges resent

A practical rule is to choose one path per file size band and test both paths with realistic documents. Teams that only test with small sample files often discover the session logic late, when a large contract fails in production.

Which permissions does the application need, and how narrow should they be?

The application needs a Graph permission that allows writing to files, plus write rights on the specific library. Choose the narrowest permission that supports the workflow, because a broad grant gives the service reach into every site in the tenant.

Delegated permissions act as the signed-in user and respect that user's SharePoint rights. Application permissions run unattended, which suits background jobs such as filing documents from an order process. Where the tenant supports site-scoped grants, prefer them over tenant-wide access. The permission names and their scopes are listed in the Microsoft Graph permissions reference.

Administrator consent is usually required for application permissions, so plan that approval into the project timeline. Do not treat a successful test with an administrator account as proof that the production identity has the right access.

Tip: Test uploads with the exact identity that runs in production, not with a developer account that already has broad rights.

How do you attach metadata to uploaded documents?

Upload the file first, then update its list item fields in a second request. The upload returns the drive item, and the associated list item carries the columns that describe the document, such as customer reference or document type.

Field internal names differ from display names, and required columns or content types can reject an otherwise valid upload. Read the library's column definitions during setup and map each business field to its internal name. If a required column has no value at upload time, the file may be left in an incomplete state, so decide whether your process sets defaults or waits for a later update.

Keep the metadata update idempotent. If the second request fails after the file arrived, a retry should set the same values again rather than create a duplicate document.

Where does this approach fail in practice?

The approach fails most often at the edges: throttling, expired tokens, conflicting names, locked files and missing folders. Each failure needs an explicit response in the application rather than a generic exception.

  • Throttling responses require retry with backoff and a cap on attempts.
  • Expired tokens must be refreshed before a long upload session begins and during it.
  • Name conflicts need a defined policy: replace, rename or version.
  • Checked-out or locked files reject writes, so the application should report them to the user or queue the work.
  • Missing folders need creation or a clear error, never a silent drop.

Versioning settings on the library also change what a replace means. If versioning is off, an overwrite destroys the previous content, which matters for any document that must be kept for audit reasons.

Hypothetical examples of filing from a business application

Here are two hypothetical scenarios:

  • An order system files a signed quotation into a customer folder after approval. The upload is small, the metadata includes a customer reference, and a failed upload is retried by a queue worker. The main design question is what happens when the same quotation is resubmitted.
  • A project tool uploads engineering drawings that are often large. It uses upload sessions, stores the session state with the job record, and resumes after a network interruption. The main design question is how long an unfinished session remains valid and who is alerted when it expires.

Neither scenario describes a real customer. They show where the design decisions usually sit: conflict policy, resumability and identity.

Frequently Asked Questions

Can Microsoft Graph upload a file to any SharePoint library?

Graph can upload to any document library that the granted permission covers, but the calling identity also needs write access to that library. The app needs both a suitable Graph permission and matching SharePoint rights. Without both, the request returns an authorisation error, so confirm access before coding.

What happens if a file with the same name already exists?

The result depends on the conflict behaviour you request and on the library's versioning settings. Decide in advance whether to replace, rename or version the file, and test that path explicitly. Silent overwrites are a common cause of lost work in document libraries.

Should the application use delegated or application permissions?

Use delegated permissions when files should be written as a signed-in user and respect that user's rights. Use application permissions for unattended background jobs, and restrict them to specific sites where possible. The choice affects audit trails, admin consent and what the service can reach.

How do I know my integration is working in production?

Log the drive item ID, the response status and the retry count for each upload. Check that the file appears in the expected folder with the expected metadata, and alert on repeated throttling or authorisation errors. A test library that mirrors production settings catches most problems earlier.

Conclusion

Storing documents in SharePoint from a business application with Microsoft Graph is straightforward once the drive model, permissions and failure paths are settled. Start with a narrow permission, resolve IDs once, choose simple or session uploads by file size, and define a conflict policy before writing code. Rui Codex offers technical implementation support for integrations of this kind, and the founder has led 200+ projects delivered. Request a project consultation to discuss how your application should file documents, or email info@ruicodex.com.

For related work, see our system integration services and ERP and CRM integration pages.

Sources

Tags: Microsoft Graph SharePoint API Integration Document Management Business Process Automation System Integration Belgium Workflow Automation Custom Software Development

Need Help Implementing This?

Our team can help you put these insights into practice. From AI automation to custom software development, we build solutions that deliver real results.

Send a project request