Modern flows authenticate through Microsoft Entra ID using MSAL. Both clients support delegated (user) and app-only (application) access.
[!IMPORTANT]
SharePoint ACS app-only access was retired in April 2026 and SAML-based authentication in May 2026. Use the certificate, interactive or device-code flows below instead — see examples/sharepoint/auth/modern/ and the migration script migrate_from_saml_to_cert.py.
[!NOTE]
SharePoint app-only access requires a certificate. A client secret is not accepted by SharePoint REST API v1 — use GraphClient for secret-based app-only. See why a client secret doesn't work for SharePoint.
from office365.sharepoint.client_context import ClientContext
ctx = ClientContext("https://contoso.sharepoint.com/sites/team").with_client_certificate(
"contoso.onmicrosoft.com",
client_id="00000000-0000-0000-0000-000000000000",
thumbprint="AA11BB22CC33DD44EE55FF66AA77BB88CC99DD00",
cert_path="./private.pem",
)
web = ctx.web.get().execute_query()
print(web.title)
from office365.sharepoint.client_context import ClientContext
ctx = ClientContext("https://contoso.sharepoint.com/sites/team").with_client_certificate(
"contoso.onmicrosoft.com", client_id, thumbprint, cert_path="./private.pem"
)
tasks = ctx.web.lists.get_by_title("Tasks")
item = tasks.add_item({"Title": "Write the release notes"}).execute_query()
print("Created item:", item.id)
for task in tasks.items.get_all().execute_query():
print(task.properties["Title"])
Microsoft Graph — send an email
from office365.graph_client import GraphClient
client = GraphClient(tenant="contoso.onmicrosoft.com").with_client_secret(client_id, client_secret)
client.me.send_mail(
subject="Hello from Graph API",
body="This email was sent using the Microsoft Graph API.",
to_recipients=["alex@contoso.onmicrosoft.com"],
).execute_query()
Examples by product
Every snippet below is lifted from a runnable script. Follow the More → link for the full example and its required permissions.
SharePoint (ClientContext)
# Lists and items
tasks = ctx.web.lists.get_by_title("Tasks")
item = tasks.add_item({"Title": "Ship it", "Status": "Active"}).execute_query()
tasks.items.get_all().execute_query() # or .filter("Status eq 'Active'").get_all()
# Files and folders
folder = ctx.web.get_folder_by_server_relative_url("/sites/team/Shared Documents")
with open("report.pdf", "rb") as f:
file = folder.files.upload(f).execute_query()
with open("report.pdf", "wb") as f:
ctx.web.get_file_by_server_relative_path("/sites/team/Shared Documents/report.pdf").download(f).execute_query()
# Large file (chunked upload session)
with open("video.mp4", "rb") as f:
folder.files.create_upload_session(f, 10 * 1024 * 1024).execute_query()
uploaded = client.me.drive.root.upload_file("report.xlsx").execute_query()
with open("report.xlsx", "wb") as f:
client.me.drive.root.get_by_path("report.xlsx").download(f).execute_query()
from datetime import datetime, timedelta, timezone
# Send mail
client.me.send_mail(
subject="Hello from Graph API",
body="This email was sent using the Microsoft Graph API.",
to_recipients=["alex@contoso.onmicrosoft.com"],
).execute_query()
# Create a calendar event
when = datetime.now(timezone.utc) + timedelta(days=1)
client.me.calendar.events.add(
subject="Team Lunch",
body="Let's grab lunch together.",
start=when,
end=when + timedelta(hours=1),
attendees=["alex@contoso.onmicrosoft.com"],
).execute_query()
with open("Sample.html", "rb") as page_html:
page = client.me.onenote.pages.add(presentation_file=page_html).execute_query()
print(page.links.oneNoteWebUrl)
devices = client.device_management.managed_devices.get().execute_query()
for device in devices:
print(device.device_name, device.operating_system, device.compliance_state)
labels = client.security.data_security_and_governance.sensitivity_labels.get().execute_query()
for label in labels:
print(label.display_name, label.id)
Addressing resources. Entities are addressed by the key you already know, so you rarely need an opaque id: user principal name, site path or URL, file path, list title. get_by_url/get_by_path on client.sites fetch when you call execute_query(); keyed/UPN forms and driveItem.get_by_path are lazy, so chain .get() first.
user = client.users["ada@contoso.com"].get().execute_query() # a user, by UPN
site = client.sites.get_by_url("https://contoso.sharepoint.com/sites/marketing").execute_query()
file = client.me.drive.root.get_by_path("Reports/Q1.xlsx").get().execute_query()
items = ctx.web.lists.get_by_title("Tasks").items.get().execute_query()
Reading large collections. Use get_all() to follow paging automatically, and page_size to stay under server limits.
Batching. Dispatch many queued operations in one round trip. Raise concurrency to run batches in parallel; throttled sub-requests are retried individually, honoring Retry-After.
for row in rows:
ctx.web.lists.get_by_title("Contacts").add_item(row)
ctx.execute_batch() # sequential
ctx.execute_batch(concurrency=5) # up to 5 batches in flight
# Graph
for user in new_users:
client.users.add(user)
client.execute_batch(concurrency=5)
Large lists (> 5,000 items). SharePoint refuses filtered/sorted queries on non-indexed columns. The library can tell you what to index and pre-flight the query.
from office365.sharepoint.listitems.caml import Caml, CamlQuery
query = CamlQuery.builder().where(Caml.text("Status").eq("Active")).order_by("ID").row_limit(2000, paged=True).build()
lst.check_query(query) # raises with a clear message if an index is missing
print(lst.index_candidates(query)) # columns worth indexing
lst.ensure_indexed("Status").execute_query() # create the index (builds in the background)
Import and export DataFrames. Optional helpers turn list items into pandas / Polars frames and back.
Every call that reaches the network has an await twin. Builders stay synchronous — only the terminal call changes. No extra dependency is required: by default the blocking HTTP call is handed to a worker thread, so the event loop stays free.
execute_query_async(), execute_batch_async(), get_all_async(), execute_query_parallel_async(), streaming downloads/exports and long-running-operation polling all have async twins, and collections support async for. See the async guide.
Dependencies
Install the core package on its own, or add the extras you need:
Use together with office365-rest-python-client: extensions, drivers, servers and companion tools.
msalPythie’s Index: 90The Microsoft Authentication Library (MSAL) for Python library enables your app to access the Microsoft Cloud by supporting authentication of users with Microsoft Azure Active Directory accounts (AAD) and Microsoft Accounts (MSA) using industry standard OAuth2 and OpenID Connect.177.3M downloads/mo