Skip to content

Microsoft SharePoint

SharePointClient

Client for interacting with Microsoft Graph SharePoint API routes.

Attributes:

Name Type Description
user_id

Unique ID for the user tethered to the client. Used to scope delegated access.

client_id

Entra client ID. Defaults to environment variable ENTRA_CLIENT_ID.

access_token str

Three-legged OAuth token for calling Graph APIs.

access_token property writable

access_token

Returns a valid signed URL, regenerating if necessary

Returns:

Type Description
str

Signed URL

__init__

__init__(user_id, access_token=None, client_id=os.getenv('ENTRA_CLIENT_ID'), client_secret=os.getenv('ENTRA_CLIENT_SECRET'))

Initialize the client.

Parameters:

Name Type Description Default
user_id str

Unique ID for the user tethered to the client. Used to scope delegated access.

required
access_token str

Three-legged OAuth token for calling Graph APIs.

None
client_id str

Entra client ID. Defaults to environment variable ENTRA_CLIENT_ID.

getenv('ENTRA_CLIENT_ID')
client_secret str

Entra client secret. Defaults to environment variable ENTRA_CLIENT_SECRET.

getenv('ENTRA_CLIENT_SECRET')

batch_get_download_urls

batch_get_download_urls(file_requests, max_workers=4)

Get multiple download URLs in parallel batch requests.

Parameters:

Name Type Description Default
file_requests List[Dict[str, str]]

List of batch requests. Each request is a dictionary matching the schema in get_download_url()

required
max_workers int

Number of parallel threads to split requests across.

4

Returns:

Type Description
Dict[str, str]

Key-value pairs of file_id and download_url.

build_folder_id_content

build_folder_id_content(site_id, drive_id, folder_id=None)

Retrieves folder contents (non-recursive) from folder_id with pagination using pooled connection.

https://learn.microsoft.com/en-us/graph/api/driveitem-list-children?view=graph-rest-1.0&tabs=http

Parameters:

Name Type Description Default
site_id str

ID of the site

required
drive_id str

ID of the drive in the site

required
folder_id str

Folder ID, child item of drive with drive_id. If not provided then method will return the children of the drive.

None

Returns:

Type Description
List[Dict[str, Any]]

List of children. Each child item has the following fields:

  • id (str): Unique Id of the drive item
  • label (str): Display name for the drive item
  • type (str): 'folder' or 'file'
  • siteId (str): ID of the site the item belongs to
  • driveId (str): ID of the parent drive
  • children: Placeholder list of children, ready for lazy loading in a UI table or tree component
  • Other attributes specific to the drive item type

build_folder_tree

build_folder_tree(site_id, drive_id, folder_path=None)

Recursively build folder tree.

Parameters:

Name Type Description Default
site_id str

ID of a SharePoint site

required
drive_id str

ID of a SharePoint drive in the site

required
folder_path str

Relative folder path from the drive root. If not provided then function will recurse from the drive root

None

Returns:

Type Description
Dict[str, Any]

Dictionary fields are:

  • id (str): ID of the drive item
  • label (str): Display name for the item
  • type (str): 'folder' or 'file'
  • children (List): List of children. Adopts the same schema.

build_tree

build_tree()

Build tree of all sites

Returns:

Type Description
Dict[str, Any]

Dictionary fields are:

  • id (str): ID of the SharePoint site
  • label (str): Display name
  • type (str): Always 'site'
  • children (List): An empty list, ready for lazy-loading when used in conjunction with `build_tree_site()

build_tree_site

build_tree_site(site_id)

Build tree for a specific site. Recurses through all drives and sub-folders to gather the full folder tree.

Parameters:

Name Type Description Default
site_id str

The ID of a SharePoint site

required

Returns:

Type Description
Dict[str, Any]

Dictionary fields are:

  • id (str): ID of the SharePoint drive
  • label (str): Display name
  • type (str): Always 'drive'
  • children (List): A list of child items. See build_folder_tree()

create_url_generator

create_url_generator(site_id, drive_id, file_id)

Create a URL generator function that uses caching and connection pooling.

Returns:

Type Description
Callable

Calling this function will regenerate the download URL for a file. Passing it to a DocumentVersion constructor will ensure the file content remains accessible if the initial download_url expires.

download_file

download_file(download_url, local_path, file_name)

Download file using pooled connection.

Parameters:

Name Type Description Default
download_url str

The direct download URL for the SharePoint file

required
local_path str

The local directory path where the file should be saved

required
file_name str

The name to use for the downloaded file

required

download_file_bytes

download_file_bytes(download_url)

Downloads a file and returns content as bytes using pooled connection. Args: download_url: The direct download URL for the file

Returns:

Type Description
bytes

The file content as bytes if successful, None if download fails

download_folder_content

download_folder_content(site_id, drive_id, folder_id, local_folder_path)

Recursively download folder contents to a local folder using pooled connection.

Parameters:

Name Type Description Default
site_id str

The ID of the SharePoint site containing the folder to download

required
drive_id str

The ID of the SharePoint drive containing the folder to download

required
folder_id str

The ID of the SharePoint folder to download

required
local_folder_path str

The local path to download files to

required

get_download_url

get_download_url(site_id, drive_id, file_id)

Get the download URL for a file.

Parameters:

Name Type Description Default
site_id str

ID of the site where the file is held

required
drive_id str

ID of the drivr where the file is held

required
file_id str

ID of the file

required

Returns:

Type Description
Optional[str]

Download URL if file is found, else None.

get_drives

get_drives(site_id)

Get drives for a site using pooled connection.

https://learn.microsoft.com/en-us/graph/api/drive-list?view=graph-rest-1.0&tabs=http

Parameters:

Name Type Description Default
site_id str

ID of the site

required

Returns:

Type Description
Dict[str, Any]

List of drives can be accessed at the dictionary's value key.

get_folder_content

get_folder_content(site_id, drive_id, folder_path=None)

Get folder contents (non-recursive) from folder_path using pooled connection.

https://learn.microsoft.com/en-us/graph/api/driveitem-list-children?view=graph-rest-1.0&tabs=http

Parameters:

Name Type Description Default
site_id str

ID of the site

required
drive_id str

ID of the drive in the site

required
folder_path str

Folder path, relative to the drive root. If not provided then method will return the children of the drive.

None

Returns:

Type Description
List[Dict[str, Any]]

List of children. Each child item has the following fields:

  • id (str): ID of the drive item
  • name (str): Name of the drive item
  • type (str): 'folder' or 'file'
  • mimeType (str): Content type of the item. Set as 'None' for folder drive items.

get_folder_id

get_folder_id(folder_contents, folder_name)

Extract folder ID from folder contents

get_raw_response

get_raw_response(item_url, json=None, headers=None, method='GET')

General-purpose method for calling Microsoft Graph API using pooled connection.

Parameters:

Name Type Description Default
item_url str

URL to call

required
json Dict

Body content

None
headers Dict

HTTP request headers

None
method str

HTTP method.

'GET'

Returns:

Type Description
Dict[str, Any]

JSON body, if present on the response

get_sites

get_sites()

Get all SharePoint sites using pooled connection.

https://learn.microsoft.com/en-us/graph/api/site-search?view=graph-rest-1.0&tabs=http

Returns:

Type Description
Dict[str, Any]

List of sites can be accessed at the dictionary's value key.