How to use Application.SharePointVersion in the xlwings API way

The SharePointVersion property of the Application object in Excel’s object model provides a read-only integer value that indicates the version of Microsoft SharePoint Foundation or Microsoft SharePoint Server that the current workbook is linked to, if it is stored on a SharePoint site. This property is particularly useful for developers who need to programmatically determine the SharePoint environment to implement version-specific features or compatibility checks when automating Excel through xlwings. In xlwings, this property is accessed via the api property, which exposes the underlying Excel object model.

Functionality:
The primary function is to identify the SharePoint version, enabling conditional logic in macros or scripts. For instance, certain features or methods may behave differently across SharePoint versions, and knowing the version allows for adaptive code. If the workbook is not stored on SharePoint, the property typically returns 0.

Syntax in xlwings:
The property is accessed through the Application object. In xlwings, after establishing a connection to Excel (usually via app = xw.App() or xw.Book), you can retrieve the value as follows:

import xlwings as xw

# Connect to the active Excel instance or create a new one
app = xw.apps.active # or xw.App() for a new instance
sharepoint_version = app.api.SharePointVersion
  • Parameters: The SharePointVersion property does not take any parameters.
  • Return Value: It returns an integer representing the SharePoint version. Common values include:
  • 0: The workbook is not stored on a SharePoint site, or SharePoint is not detected.
  • 14: Corresponds to SharePoint 2010.
  • 15: Corresponds to SharePoint 2013.
  • 16: Corresponds to SharePoint 2016 or SharePoint Online (Office 365).
  • Other integer values may represent different or future versions.

Example Usage:
Below is a practical xlwings code example that checks the SharePoint version and performs actions based on the result. This example assumes Excel is already running with a workbook open, possibly from a SharePoint location.

import xlwings as xw

def check_sharepoint_version():
# Get the active Excel application
app = xw.apps.active

# Retrieve the SharePoint version
version = app.api.SharePointVersion

# Display or use the version information
if version == 0:
    print("This workbook is not stored on SharePoint.")
elif version == 14:
    print("SharePoint 2010 detected. Implement compatibility for this version.")
# Add version-specific code here, e.g., adjust data connection settings
elif version == 15:
    print("SharePoint 2013 detected. Features for this version are available.")
elif version == 16:
    print("SharePoint 2016 or SharePoint Online detected. Use modern APIs.")
else:
    print(f"Unknown SharePoint version: {version}. Check for updates.")

# You can also use the value in conditional logic for further automation
if version >= 16:
# Example: Enable newer SharePoint integration features
    print("Proceeding with advanced SharePoint functionalities.")
    return version

# Run the function
if __name__ == "__main__":
    sharepoint_ver = check_sharepoint_version()
    print(f"SharePoint Version Code: {sharepoint_ver}")

April 20, 2026 (0)


Leave a Reply

Your email address will not be published. Required fields are marked *