OneDrive File Issues

How to Detect When a Shared Excel Workbook is in Use via API

Ayan MasoodAyan Masood Oct 7, 2026 870 views

Question details

The user needs a programmatic method to detect whether a shared Excel workbook synchronized via OneDrive and SharePoint is currently in use, in order to prevent sync conflicts in a C# application.

How to Detect When a Shared Excel Workbook is in Use via API
Product
Microsoft Excel / OneDrive API
Device & OS
not provided
Scenario
A custom C# application is reading and writing to Excel files hosted on OneDrive/SharePoint, but simultaneous edits from other users or instances are causing synchronization conflicts.
Observed behavior
The application fails to recognize that the shared workbook is already in use by another user, leading to overlapping writes and synchronization conflicts.
Before you start

Before modifying your application code, ensure that your OneDrive or SharePoint synchronization client is fully updated and that your C# environment has the appropriate permissions to query file metadata or Microsoft Graph API endpoints.

Solution 1Recommended

Method 1: Check File Locks using C# FileStream (System.IO)

Use standard C# exception handling to attempt exclusive access to the Excel file before reading or writing, which helps determine if the file is currently locked by another process.

The most straightforward way to check if an Excel file is in use locally (or via a mapped OneDrive sync folder) is to attempt to open the file with exclusive read/write access. If another user or application is editing the file, the operating system will throw an IOException.

1
Open your C# Project

Launch your IDE (such as Visual Studio) and navigate to the code module handling file input/output for the Excel workbook.

2
Implement a Try-Catch Block

Write a method using File.Open(filePath, FileMode.Open, FileAccess.ReadWrite, FileShare.None) to attempt opening the target Excel file.

3
Catch the IOException

Add a catch block for System.IO.IOException. If caught, this confirms the file is currently locked or in use. You can then prompt your application to wait, retry after a delay, or alert the user rather than forcing a write that causes a sync conflict.

Method 1: Check File Locks using C# FileStream (System.IO)
Local Sync Limitations: This method works best for files synced to a local drive. For direct cloud interactions without local syncing, consider using API-level checks.
Free Microsoft Office alternative

Use WPS Office for Seamless API Integration and Spreadsheet Management

If you are experiencing frequent synchronization conflicts and complex API locking issues with Microsoft Excel, consider switching to WPS Office. It offers robust spreadsheet management, lightweight architecture, and developer-friendly integrations as a highly compatible alternative to Microsoft Office.

  1. 1. Download the Installer: Visit the official WPS Office website and download the free installation package for your operating system.
  2. 2. Install WPS Office: Run the downloaded executable and follow the on-screen prompts to complete the installation.
  3. 3. Open Your Spreadsheets: Launch WPS Spreadsheets and open your existing .xlsx files to enjoy smooth, conflict-free editing and management.
Highly compatible with Microsoft Excel (.xlsx, .xls, .csv) formats to ensure your data stays intact.Provides extensive OpenAPI and COM support for custom software development and file handling.Lightweight architecture ensures faster load times and reduces heavy local synchronization conflicts.Free to use with a familiar, user-friendly tabbed interface for seamless migration.
QA img-9

Frequently Asked Questions

Why do OneDrive sync conflicts occur when editing Excel files via C#?

Conflicts happen when a programmatic script (like a C# app) and a human user—or two different scripts—attempt to write to the same OneDrive-synced file simultaneously without using a co-authoring API or implementing a proper file lock mechanism.

Can the standard Excel Interop API detect shared users?

The standard Excel COM Interop API has properties such as `Workbook.UserStatus`, which returns a 2D array of users who currently have the workbook open as a legacy shared list. However, this is often unreliable for modern OneDrive or SharePoint co-authoring sessions.

Is there a way to lock an Excel file on SharePoint via API?

Yes, you can use the Microsoft Graph API or the SharePoint REST API to check out a file. Checking out a file places a lock on it, preventing other users or applications from modifying it while your C# application processes its data.