Sample code for 30+ languages & platforms
Chilkat2-Python Requires Chilkat v11.0.0+

SharePoint Get Files in Root Folder

See more SharePoint Examples

Gets the list of files that exist in the root folder.

Chilkat Chilkat2-Python Downloads

Chilkat2-Python
import sys
import chilkat2

success = False

# This requires the Chilkat API to have been previously unlocked.
# See Global Unlock Sample for sample code.

http = chilkat2.Http()

# --------------------------------------------------------------------------------------------------------
# Provide the information needed for Chilkat to automatically fetch the OAuth2.0 access token as needed.

# To create your App Registration for SharePoint in Azure Entra ID,
# see How to Create SharePoint App Registration for OAuth 2.0 Client Credentials

jsonOAuthCC = chilkat2.JsonObject()
jsonOAuthCC.UpdateString("client_id","CLIENT_ID")
jsonOAuthCC.UpdateString("client_secret","SECRET_VALUE")
jsonOAuthCC.UpdateString("scope","https://graph.microsoft.com/.default")
jsonOAuthCC.UpdateString("token_endpoint","https://login.microsoftonline.com/TENANT_ID/oauth2/v2.0/token")

http.AuthToken = jsonOAuthCC.Emit()
# --------------------------------------------------------------------------------------------------------

# Indicate that we want a JSON reply
http.Accept = "application/json;odata=verbose"

http.SetUrlVar("sharepoint_hostname","example.sharepoint.com")
sbJson = chilkat2.StringBuilder()
success = http.QuickGetSb("https://graph.microsoft.com/v1.0/sites/SITE_ID/drive/root/children",sbJson)
if (success == False):
    print(http.LastErrorText)
    sys.exit()

json = chilkat2.JsonObject()
json.LoadSb(sbJson)

# Iterate over the results and get each file's name, size, and last-modified date/time.

numFiles = json.SizeOfArray("d.results")
print("Number of Files in the SharePoint root folder = " + str(numFiles))

i = 0
while i < numFiles :
    json.I = i

    filename = json.StringOf("d.results[i].Name")
    fileRelativeUri = json.StringOf("d.results[i].ServerRelativeUrl")
    fileSize = json.IntOf("d.results[i].Length")

    print(str(i + 1) + ": " + filename)
    print("    Relative URI: " + fileRelativeUri)
    print("    Size in Bytes: " + str(fileSize))

    i = i + 1

# The output of this program when I tested it: