Skip to main content

Common tasks with SharePoint REST service

Project description

GitHub Actions CI PyPI - License PyPI - Version PyPI - Wheel Supported Versions

Sppyte

A tiny, explicit Python helper for working with SharePoint site in Python using legacy SharePoint REST endpoints using and NTLM authentication. Sppyte[^1] keeps a very thin abstraction so you can reason about the underlying HTTP calls without surprises.

⚠️ This client uses NTLM (requests-ntlm) and is best suited for SharePoint on-prem or environments where NTLM is configured. SharePoint Online typically uses different auth flows.

Table of Contents

Features

  • Simple Site connection with NTLM auth and automatic form digest retrieval.
  • List helper to:
    • Add / update / delete items
    • Use OData params to control item responses
    • Add attachments to items
  • Library helper to:
    • Add / delete folders
    • Upload / download / delete documents
    • List folder contents and control output with OData params
  • Access other SharePoint REST service endpoints as needed

Installation

pip install sppyte

Quickstart

from sppyte import Site

HOST = "https://sharepoint.example.com"
SITE = "/sites/parrots" # relative path
USER = "norweigian"
PASS = "••••••••"

with Site(HOST, SITE, USER, PASS) as site:
    # ---------------- Lists ----------------
    pets = site.list("Pets")

    # Add an item (metadata type is auto-inferred if omitted)
    new_id = pets.add_item({"Title": "Norweigian Blue"})

    # Update the item (uses MERGE)
    patch = {"Title": "Polly"} # Fields to be updated
    pets.update_item(new_id, patch)

    # Attach a file
    with open("notes.txt", "rb") as file_handle: # Read bytes
        pets.add_attachment(new_id, "notes.txt", file_handle)

    # Fetch one item
    item = pets.get_item(new_id)

    # Query contents (use any OData params you need)
    params = {
        "$select": "Id,Title,Created",
        "$top": 5,
        "$orderby": "Created desc",
    }
    r = pets.get_contents(params)

    # Delete the item
    pets.delete_item(new_id)

    # --------------- Libraries -------------
    shared_docs = site.library("Shared Documents")

    # Ensure a nested folder path exists
    shared_docs.add_folder("Napping", "2025")

    # Upload a document
    with open("report.pdf", "rb") as file_handle:
        unique_id = shared_docs.add_document("report.txt", file_handle)

    # List library files
    params = {"$select": "Name,TimeCreated"}
    files = docs.list_contents(params)

    # List files in subfolder
    folder_files = docs.list_contents(params, "Napping", "2025")

    # Download a document
    report = docs.get_document("report.txt")
    contents = report.decode()

    # Download a document in a subfolder (ex: Shared Documents/Napping/2025/report.txt)
    report = docs.get_document("report.txt", "Napping", "2025")

    # Delete a document
    docs.delete_document("report.txt")

    # Delete a document in a subfolder
    docs.delete_document("report.txt", "Napping", "2025")

API Overview

Site

Model a SharePoint site with authentication and session management.

sppyte.Site(host, site, username, password)

Configure a SharePoint site connection.

Parameters

  • host: str - SharePoint site host (protocol://domain)
  • site: str - SharePoint site relative url
  • username: str - SharePoint site username
  • password: str - SharePoint site password

Returns: Site object

Usage:

from sppyte import Site

HOST = "https://sharepoint.example.com"
SITE = "/sites/parrots" # relative path
USER = "norweigen"
PASS = "••••••••"

site = Site(HOST, SITE, USER, PASS)
try:
  connection.connect() # Start session
  # Do cool stuff
except HTTPError as e:
  # Deal with errors
finally:
  connection.close() # Close session

# With context managment is preferred
with Site(HOST, SITE, USER, PASS) as connection:
  # Do cool stuff

Notes

  • User should have permissions to the site, library, or list to be accessed. Updates require contribute or higher level access. Unauthorized user will raise a requests.HTTPError 401 - Unauthorized exception.
  • For user managed connections, call the connect method to start a session and close method to end the session. Using a with statement for context management will automatically connect and close sessions.

Site.connect()

Start a connected session.

Site.close()

Close connected session.

Site.list(name)

Get a List class instance.

Parameters

  • name: str - SharePoint list name

Returns: sppyte.List

Usage:

# Given a list named 'Pets'
pets = connection.list('Pets')

Site.library(name)

Get a Library class instance.

Parameters

  • name (str): SharePoint document library name

Returns: sppyte.Library

Usage:

# Given a document library named 'Contracts'
contracts = connection.library('Contracts')

List

Model a SharePoint list with authentication and methods to interact with the list items.

sppyte.List(name, site)

Create a list connection. Useful alternative when you only need to access a single list.

Parameters

  • name (str): SharePoint list name
  • site (Site): sppyte Site

Returns: List object

Usage:

from sppyte import Site, List

HOST = "https://sharepoint.example.com"
SITE = "/sites/parrots" # relative path
USER = "norweigian"
PASS = "••••••••"

# Given a list named 'Pets'
site = Site(HOST, SITE, USER, PASS)
pets = List('Pets', site)

try:
  pets.connect()
  # Do cool stuff
except HTTPError as e:
  # Deal with errors
finally:
  pets.close() # Close session

# With context managment is preferred
with List('Pets', site) as pets:
  # Do cool stuff

Notes

  • User should have permissions to the list to be accessed. Updates require contribute or higher level access. Unauthorized user will raise a requests.HTTPError 401 - Unauthorized exception.
  • For user managed connections, call the connect method to start a session and close method to end the session. Using a with statement for context management will automatically connect and close sessions.

List.connect()

Start a connected session.

List.close()

Close the session.

List.add_item(item)

Add an item to a SharePoint list. Returns the added item ID.

Parameters

  • item (dict[str, str | int]): Item to be added

Returns: int

Usage:

# Given a item with Title and breed fields
new_item = {
  "Title": "Sonny",
  "breed": "Norwiegen Blue"
}
pet_id = pets.add_item(new_item)

Notes

  • Required SharePoint metadata is added automatically.

List.add_attachment(sp_id, file_name, attachment)

Add an attachment to an existing list item.

Parameters

  • sp_id: int - Item to append attachment
  • file_name: str - Attachment file name (include file extension)
  • attachment: bytes | IO[bytes] - Stream file content

Returns: int - SharePoint item id

Usage:

# Given a item with ID of 23
pet_id = 23
with open("notes.txt", "rb") as fh:
    pets.add_attachment(pet_id, "notes.txt", fh)

List.delete_item(sp_id: int)

Parameters

  • sp_id: int - Item to delete

Returns: bool

Usage:

# Given a item with ID of 23
pet_id = 23
delete_success = pets.delete_item(pet_id)

List.get_contents(params)

Get contents of a SharePoint list.

Parameters params: dict[str, str | int] - OData params (See the docs for supported OData params)

Returns: list[dict[str, str | int]] - JSON decoded list items

Usage:

pet_items = pets.get_contents({
    "$select": "Id,Title,Created",
    "$top": 500,
    "$orderby": "Created desc",
})

List.get_item(sp_id)

Get SharePoint list item by ID.

Parameters

  • sp_id: int - SharePoint ID to retrieve

Returns: dict[str, str | int] - JSON decoded item contents

Usage:

pet_id = 23
pet_item = pets.get(pet_id)

List.update_item(sp_id: int, patch: dict)

Update an existing list item merging properties from a patch.

Parameters

  • sp_id: int - SharePoint ID to update
  • patch: dict[str, str | int] - Dictionary of fields and values to update

Returns: int - Updated SharePoint ID

Usage:

pet_id = 23
patch = {
  "Title": "Polly",
}
pets.update_item(pet_id, patch)

Library

Model a SharePoint library with authentication and methods to interact with the documents.

sppyte.Library(name, site)

Connect to a document library. Useful alternative when you only need to access a single library.

Parameters

  • name (str): SharePoint library name
  • site (Site): sppyte Site

Returns: Library object

Usage:

from sppyte import Site, Library

HOST = "https://sharepoint.example.com"
SITE = "/sites/Parrots" # relative path
USER = "norweigen"
PASS = "••••••••"

# Given a document library named 'Contracts'
site = Site(HOST, SITE, USER, PASS)
contracts = Library('Contracts', site)

try:
  contracts.connect()
  # Do cool stuff
except HTTPError as e:
  # Deal with errors
finally:
  contracts.close() # Close session

# With context managment is preferred
with Library('Contracts', site) as contracts:
  # Do cool stuff

Notes

  • User should have permissions to the library to be accessed. Updates require contribute or higher level access. Unauthorized user will raise a requests.HTTPError 401 - Unauthorized exception.
  • For user managed connections, call the connect method to start a session and close method to end the session. Using a with statement for context management will automatically connect and close sessions.

Library.connect()

Start a connected session.

Library.close()

Close the session.

Library.add_folder(folder, *subfolders)

Add a folder to a SharePoint document library.

Parameters

  • folder: str - folder to add
  • *subfolders: str (Optional) - additional path folder names for nested folders

Returns: bool - Add succeeded

Usage:

contracts.add_folder('2025', 'January')

Library.add_document(file_name, document, *subfolders)

Load a document to a SharePoint document library.

Parameters

  • file_name: str - file name add
  • document: bytes | IO[bytes] - Streamed file content
  • *subfolders: str (Optional) - additional path folder names for nested folders

Returns: str - Unique ID

Usage:

with open("notes.txt", "rb") as file_handler:
    contracts.add_document("notes.txt", file_handler, '2025', 'January')

Library.folder_exists(folder, *subfolders)

Check if a folder exists in a SharePoint document library.

Parameters

  • folder: str - Folder to add
  • *subfolders: str (Optional) - Additional path folder names for nested folders

Returns: bool - Folder exists

Usage:

contracts.folder_exists('2025', 'January')

Library.delete_document(file_name, *subfolders)

Delete a document from a SharePoint document library.

Parameters

  • file_name: str - File name to delete
  • *subfolders: str (Optional) - Additional path folder names for nested folders

Returns: bool - Delete succeeded

Usage:

contracts.delete_document("notes.txt", '2025', 'January')

Library.delete_folder(folder, *subfolders)

Delete a folder from a SharePoint document library.

Parameters

  • folder: str - Folder to add
  • *subfolders: str (Optional) - Additional path folder names for nested folders

Returns: bool - Delete succeeded

Usage:

contracts.delete_folder('2025', 'January')

Library.list_contents(params, *subfolders)

List contents of a SharePoint document library.

Parameters

  • params: dict[str, str | int] - OData params (See the docs for supported OData params)
  • *subfolders: str (Optional) - Additional path folder names for nested folders

Returns: list[dict[str, Any]] - JSON decoded list metadata

Usage:

files = contracts.list_contents({
    "$select": "Name,TimeCreated"
  }, "January", "2025")

Library.get_document(file_name, *subfolders)

Read a document from a SharePoint document library.

Parameters

  • file_name: str - File name to delete
  • *subfolders: str (Optional) - Additional path folder names for nested folders

Returns: bytes - Streamed contents

Usage:

document = contracts.get_document("notes.txt", "2025", "January")
contents = document.decode()

Errors

  • SessionError: raised when an HTTP session isn’t available.
  • ResponseFormatError: raised when an expected JSON field is missing (e.g., FormDigestValue, ListItemEntityTypeFullName, ID, UniqueId).

Handle them as you would any exception:

from sppyte import ResponseFormatError, SessionError

try:
    ...
except (ResponseFormatError, SessionError) as e:
    print("Sppyte error:", e)

Extension methods

SharePoint REST services endpoints not explicitly implemented can be accessed through the request method exposed on Site. This methods uses authentication from the current Site session and shadows requests.request from the requests package, using a site relative path instead of a complete url.

The get_form_digest method is provided to obtain the bearer token passed in the X-RequestDigest header for update requests.

List exposes the get_item_type method to obtain the required list item type metadata for list updates.

Notes

  • OData Parameters: Methods like get_contents and list_contents accept any OData parameters via params (e.g., $select, $filter, $orderby, $top). See the docs for supported OData params.
  • HTTP request errors are passed through from the requests library unhandled for transparency. See the docs for more information.
  • SharePoint limits list response record counts by default. Use the $top OData param for larger response counts.

Contributing

This is a pet project to save me time at work and not open for contribution.

Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.

Authors

License

MIT License

[^1]: What's the deal with this name? (S)hare(P)oint (Py)thon Si(te)

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

sppyte-0.1.7.tar.gz (11.5 kB view details)

Uploaded Source

Built Distribution

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

sppyte-0.1.7-py3-none-any.whl (11.4 kB view details)

Uploaded Python 3

File details

Details for the file sppyte-0.1.7.tar.gz.

File metadata

  • Download URL: sppyte-0.1.7.tar.gz
  • Upload date:
  • Size: 11.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for sppyte-0.1.7.tar.gz
Algorithm Hash digest
SHA256 4b285aaad1c3d91fab8c4f70a6569b2a612b78f772af466dedcbf471e0f7635d
MD5 0736d13e9fdaeb86f2ce4ecab1513b81
BLAKE2b-256 aea688e2fda62daeda21b90414c3c9212878aea66caab1389bb2f875401d56ae

See more details on using hashes here.

Provenance

The following attestation bundles were made for sppyte-0.1.7.tar.gz:

Publisher: publish.yml on B-Jones-RFD/sppyte

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sppyte-0.1.7-py3-none-any.whl.

File metadata

  • Download URL: sppyte-0.1.7-py3-none-any.whl
  • Upload date:
  • Size: 11.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for sppyte-0.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 eb0f09bde1a921872087303f775894149f2da4327647f00fb59a2bfd5953abb3
MD5 3ab0b18b47e41927f3901a5765d3084b
BLAKE2b-256 32a2d31d7362970c2abe067ee748b88e62e15b7c0cd01d4aeab6efc9ec6e60a8

See more details on using hashes here.

Provenance

The following attestation bundles were made for sppyte-0.1.7-py3-none-any.whl:

Publisher: publish.yml on B-Jones-RFD/sppyte

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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