Skip to main content

No project description provided

Project description

SharePoint API Python Client – Documentation

A modern Python client for SharePoint REST API operations using a client pattern for better abstraction.

Features

  • Site-scoped operations through SharePointSite client
  • User management via SharePointUser
  • List item operations with SharePointListItem
  • NTLM authentication support
  • Comprehensive error handling
  • Metadata discovery and batch operations

Installation

pip install sharepoint-v1-api

From source:

git clone https://github.com/your-org/nc-devops-sharepoint-v1-api.git
cd nc-devops-sharepoint-v1-api
pip install -e .

Authentication & Initialization

import requests
from requests_ntlm import HttpNtlmAuth
from sharepoint import SharePointSite

# Create authenticated session
session = requests.Session()
session.auth = HttpNtlmAuth('your_user', 'your_password')
session.proxies = {}  # Add proxies if needed

# Initialize site client
site = SharePointSite(
    url="https://your.sharepoint.com/_api/Web",
    session=session
)

Core Operations

List Operations

# Get all lists
all_lists = site.get_lists()

# Get list by title
tasks_list = site.get_list(list_title="Tasks")

# Filter items with OData syntax
active_tasks = tasks_list.get_items(
    filters="Status eq 'Active'",
    select_fields=["Title", "DueDate"]
)

# Get items with pagination (for large lists)
# First page
first_page = tasks_list.get_items_paged(top=100)
for item in first_page["items"]:
    print(f"Item ID: {item.id}")

# Get next page using the skiptoken
if first_page["has_next"]:
    second_page = tasks_list.get_items_paged(
        top=100,
        skiptoken=first_page["next_skiptoken"]
    )

# Iterate through all items in a large list automatically
for item in tasks_list.iterate_all_items(page_size=1000):
    print(f"Processing item {item.id}")

User Management

# Get user by ID
user = site.get_user(42)
print(f"User: {user.Title} ({user.Email})")

List Items

# Get specific item
task_item = tasks_list.get_item(101)

# Update item
update_data = {
    "Status": "Completed",
    "PercentComplete": 1.0
}
task_item.update_item(update_data)

# Check modification history
print(f"Last modified: {task_item.modified}")

Advanced Usage

Field Management

# Get list fields
fields = tasks_list.get_fields()
required_fields = [f for f in fields if f.required]

# Update field properties (requires 'Manage Lists' permission)
try:
    description_field = next((f for f in fields if f.title == "TaskDescription"), None)
    if description_field:
        description_field.update_field({
            "Description": "Max length increased to 500 characters",
            "MaxLength": 500
        })
except PermissionError:
    print("Update failed - insufficient permissions")

# Delete obsolete field (irreversible operation)
if description_field and not description_field.required and description_field.hidden:
    success = description_field.delete_field()
    if success:
        print(f"Field '{description_field.title}' deleted")
    else:
        print("Field deletion failed")
else:
    print("Skipping deletion - field is required or visible")
    
# Check field metadata
if description_field:
    print(f"Field '{description_field.title}' type: {description_field.type_as_string}")
    print(f"Hidden: {description_field.hidden}, Read-only: {description_field.read_only}")

Site Metadata

print(f"Site Title: {site.title}")
print(f"Created Date: {site.created.strftime('%Y-%m-%d')}")
print(f"Default Language: {site.language_name}")

Working with Large Lists (Pagination)

SharePoint has list view thresholds that prevent retrieving too many items at once. When working with large lists, use the pagination methods:

# For manual pagination control
page_result = tasks_list.get_items_paged(top=1000)
items = page_result["items"]
has_next = page_result["has_next"]
next_token = page_result["next_skiptoken"]

# For automatic pagination through all items
for item in tasks_list.iterate_all_items(page_size=1000):
    # Process each item
    print(f"Processing item {item.id}")

Batch Operations

# Get multiple lists with filtering
recent_lists = site.get_lists(
    filters="Created gt datetime'2023-01-01T00:00:00Z'",
    select_fields=["Title", "ItemCount"],
    top=10
)

Error Handling

from requests.exceptions import ConnectionError

try:
    site.get_lists()
except ConnectionError as e:
    print(f"Network error: {e}")
except PermissionError:
    print("Authentication failed - check credentials")
except FileNotFoundError:
    print("Requested resource not found")

Class Reference

Class Description Key Methods/Properties
SharePointSite Main entry point for site operations get_lists, get_folder, get_user
SharePointUser User profile management Email, UserName, Title
SharePointList List operations and metadata get_items, get_items_paged, iterate_all_items, get_item, get_fields
SharePointListItem Individual item manipulation update_item, delete_item
SharePointFolder File/folder operations get_folders, get_folder
SharePointListField Field metadata and validation update_field, delete_field, title, type_as_string, required

Best Practices

  1. Reuse Sessions: Create one SharePointSite instance per site and reuse it
  2. Selective Loading: Use select_fields parameter to optimize payload sizes
  3. Error Recovery: Implement retry logic for transient network errors
  4. Field Validation: Check required and read_only properties before updates
  5. Metadata Caching: Cache frequently accessed metadata like list GUIDs

Migration from v0.x

# Old style (deprecated)
from sharepoint_api import SharePointAPI
api = SharePointAPI._compact_init(creds)
items = api.get_lists("cases")

# New client pattern
from sharepoint import SharePointSite
site = SharePointSite(url=site_url, session=session)
items = site.get_lists()

License

MIT License - See LICENSE for details.

Changelog

1.0.2 – 2026-01-22

Added

  • Pagination support for SharePoint list items using $skiptoken
  • New methods in SharePointList: get_items_paged() and iterate_all_items()
  • Support for skiptoken parameter in OData query building

Documentation

  • Updated README.md to accurately reflect current implementation
  • Fixed parameter names and method references in examples
  • Removed references to non-existent functionality
  • Corrected class reference table with accurate method names

1.0.1 – 2025-01-16

Changed

  • Fix documentation, using url instead of site_url

1.0.0 – 2025-12-15

Added

  • New client classes: SharePointSite, SharePointUser, SharePointList, SharePointListItem, SharePointFolder
  • SharePointListField class for field-level metadata and operations
  • Comprehensive type hints throughout the codebase
  • Lazy-loaded metadata pattern for all client objects
  • Batch operation support for list items and fields
  • New documentation examples for field management

Deprecated

  • SharePointAPI class and _compact_init method - replaced by client pattern
  • Legacy method names using camelCase (e.g. getListByName -> get_list_by_name)

Changed

  • BREAKING: Complete restructuring into client pattern
  • Unified snake_case naming convention for all methods and properties
  • Improved error handling with specific exception types (PermissionError, FileNotFoundError)
  • Updated documentation with migration guide and new API examples
  • Revised authentication flow to use standard requests.Session

Security

  • Enhanced validation for GUID parameters
  • Stricter type checking for all API inputs
  • Mandatory form digest validation for write operations

0.2.9 – 2025-12-11

Added

  • Using a pre-defined session when initializing.
  • Support for merging session-level headers in API calls, preserving custom session headers.

Changed

  • Added deprecation warnings for _compact_init.

0.2.8 – 2025-12-05

Added

  • Added a central datetime handler method to ensure timezone-aware datetimes.

0.2.6 – 2025-12-05

Added

  • Added select_fields option to the get_site_metadata method

0.2.5 – 2025-12-05

Changed

  • Replace _resolve_sp_list with _resolve_sp_list_url returning a URL fragment, simplifying GUID handling.
  • Update all API calls to use the new URL fragment across list retrieval, item operations, and metadata fetching.
  • Simplify SharePointListItem constructor by removing explicit list_guid argument and adding a lazy list_guid property that extracts GUID from the parent list URI.

0.2.4 – 2025-12-03

Added

  • New high-level SharePointSite object providing lazy-loaded site metadata

0.2.3 – 2025-12-02

Added

  • GUID validation for list identifiers with fallback to list title lookup via GetByTitle.
  • New helper method _is_valid_guid in SharePointAPI.
  • Centralized list resolution helper _resolve_sp_list for GUID, title, or SharePointList instances.
  • New method get_list_metadata to fetch only list metadata without items.
  • Type hint enhancements: added Optional, Tuple imports; updated method signatures (e.g., get_item_versions now uses Optional[List[str]]).
  • Updated get_list to return the resolved SharePointList object after appending items.
  • Refactored multiple methods (get_item, create_item, update_item, attach_file, get_item_versions, get_case) to use _resolve_sp_list, removing redundant GUID validation logic.

Changed

  • Cleaned up stray else: blocks and syntax errors.
  • Improved consistency of return objects across list-related methods.
  • Updated imports and docstrings accordingly.

0.2.2 – 2025-11-28

Added

  • SharePointList.fields property to retrieve list field definitions

0.2.1 – 2025-11-06

Added

  • Optional select_fields parameters to list retrieval methods for more efficient queries.
  • New public API methods:
    • SharePointAPI.get_group_users – fetch users of a SharePoint group.
  • Improved error handling with explicit TypeError exceptions.
  • Detailed docstrings for core classes and methods (enhances IDE support).

Changed

  • HTTP header handling unified; corrected X-HTTP-Method: PUT for full updates.
  • Error handling improved: generic sys.exit(1) replaced with explicit TypeError/ConnectionError exceptions.

Fixed

  • Fixed incorrect PUT header that previously sent a MERGE header.
  • Minor docstring formatting issues.

Security

  • Enforced NTLM authentication across all request helpers.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sharepoint_v1_api-1.0.2.tar.gz (29.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sharepoint_v1_api-1.0.2-py3-none-any.whl (24.9 kB view details)

Uploaded Python 3

File details

Details for the file sharepoint_v1_api-1.0.2.tar.gz.

File metadata

  • Download URL: sharepoint_v1_api-1.0.2.tar.gz
  • Upload date:
  • Size: 29.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for sharepoint_v1_api-1.0.2.tar.gz
Algorithm Hash digest
SHA256 c17c33c6525f0212641cf470b001e352cc9fd2e4983853e04e67a7993167d0da
MD5 cb68349321960fb7321ff762d6980385
BLAKE2b-256 f9ea3a61b3c828603285365dd8b1aea0f8ad5a22509e0ea09e7b3a4ce91f7ec1

See more details on using hashes here.

File details

Details for the file sharepoint_v1_api-1.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for sharepoint_v1_api-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 f0b447c29289181deef2f7459ce6e4f9447e3142f5c895107b4a1f098cb42b3f
MD5 96d81c6b623aac5d56cf56fef7cb9137
BLAKE2b-256 33680b41210db5f0e3fa20983572b4931cfa7233f8bba7cabbb4e1b11f5c4eca

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page