Skip to content

Upload, download, list, move and delete files in SharePoint Online document libraries (Microsoft Graph). **Setup**: add project secrets for one of these sign-in methods. `Sharepoint.connect(siteUrl)` picks the method from the secrets that exist. - *Service account*: `sharepoint_username` (e.g. `svc-reflex@contoso.com`) and `sharepoint_password`. The account must be excluded from multi-factor authentication. Optionally `sharepoint_client_id`: an app registration with delegated `Sites.ReadWrite.All` (admin consent) and *Allow public client flows* enabled. Without it, the Microsoft Office client id is used, which some tenants block. - *App registration*: `sharepoint_client_id` and `sharepoint_client_secret`, for an app with the application permission `Sites.ReadWrite.All` (or `Sites.Selected` plus a grant on the site) and admin consent. The tenant is derived from the site URL; set `sharepoint_tenant_id` only if that fails. Sign-in errors explain what to change (wrong password, MFA required, consent missing, ...). When both kinds of secrets exist, `Sharepoint.connect(siteUrl)` uses the app registration. connect makes network calls (sign-in and resolving the site), so wrong credentials or a wrong URL fail there. Only SharePoint Online (*.sharepoint.com) sites are supported. There is no connection to close. Every request has a 5 minute timeout; errors are raised as "SharePoint <action> failed: HTTP <status> <code>: <message>". Remote paths are relative to the document library (no site URL, no library name). Local paths are relative to the project. Functions that return `this` can be chained: `site.createFolder('A').upload('a.txt', 'A/a.txt')`.

Functions

  • connect SharepointSite Signs in and opens the default document library of a site (usually "Documents", shown as "Shared Documents" in the URL); switch with useLibrary.
  • connectUser SharepointSite Signs in with a service account (user name and password, without multi-factor authentication) and opens the default document library of a site.
Sharepoint.connect(siteUrl: string)→SharepointSiteSharepoint.connect(tenantId: string, clientId: string, clientSecret: string, siteUrl: string)→SharepointSite

Signs in and opens the default document library of a site (usually "Documents", shown as "Shared Documents" in the URL); switch with useLibrary. With only a site URL, the credentials come from the project secrets (see the setup above): the app registration when sharepoint_client_secret exists, otherwise the service account. With four arguments, signs in as the given app registration (client credentials) and ignores the secrets. Raises an error that explains what to change when the sign-in fails.

Parameters

tenantId string
Directory (tenant) id or domain, e.g. contoso.onmicrosoft.com
clientId string
Application (client) id of the app registration
clientSecret string
Client secret of the app registration, e.g. secret('my_secret')
siteUrl string
Site URL, e.g. https://contoso.sharepoint.com/sites/Finance

Returns

SharepointSite A SharePoint site and document library.

Example

local site = Sharepoint.connect("https://contoso.sharepoint.com/sites/Finance")
site.createFolder("Reports/2026")
site.upload("output/summary.xlsx", "Reports/2026/summary.xlsx")
for _, item in ipairs(site.list("Inbox")) do
if not item.folder then
site.download(item.path, "input/" .. item.name)
site.move(item.path, "Processed/" .. item.name)
end
end
Sharepoint.connectUser(username: string, password: string, siteUrl: string)→SharepointSite

Signs in with a service account (user name and password, without multi-factor authentication) and opens the default document library of a site. Uses sharepoint_client_id and sharepoint_tenant_id from the project secrets when present, otherwise the Microsoft Office client id and the tenant derived from the site URL. Use this instead of connect when the credentials are not stored under the standard secret names.

Parameters

username string
Username, e.g. svc-reflex@contoso.com
password string
Password, e.g. secret('sharepoint_password')
siteUrl string
Site URL, e.g. https://contoso.sharepoint.com/sites/Finance

Returns

SharepointSite A SharePoint site and document library.

Example

local site = Sharepoint.connectUser("svc-reflex@contoso.com", secret("svc_password"), "https://contoso.sharepoint.com/sites/Finance")
print(site.exists("Reports"))