How to Detect When a Shared Excel Workbook is in Use via API
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.

- 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 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.
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.
Launch your IDE (such as Visual Studio) and navigate to the code module handling file input/output for the Excel workbook.
Write a method using File.Open(filePath, FileMode.Open, FileAccess.ReadWrite, FileShare.None) to attempt opening the target Excel file.
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 2: Use Microsoft Graph API to Check File Status
If the file is hosted exclusively on SharePoint or OneDrive, query the Microsoft Graph API to check the file's current lock status or active collaboration sessions.
Method 3: Consult the Office Development Community
For deep programmatic integration issues involving specific Excel COM Interop APIs and SharePoint shared workbooks, Microsoft's dedicated development channels offer specialized guidance.
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. Download the Installer: Visit the official WPS Office website and download the free installation package for your operating system.
- 2. Install WPS Office: Run the downloaded executable and follow the on-screen prompts to complete the installation.
- 3. Open Your Spreadsheets: Launch WPS Spreadsheets and open your existing .xlsx files to enjoy smooth, conflict-free editing and management.

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.




