Tcl
Tcl
SharePoint Get Files in Root Folder
See more SharePoint Examples
Gets the list of files that exist in the root folder.Chilkat Tcl Downloads
load ./chilkat.dll
set success 0
# This requires the Chilkat API to have been previously unlocked.
# See Global Unlock Sample for sample code.
set http [new_CkHttp]
# --------------------------------------------------------------------------------------------------------
# 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
set jsonOAuthCC [new_CkJsonObject]
CkJsonObject_UpdateString $jsonOAuthCC "client_id" "CLIENT_ID"
CkJsonObject_UpdateString $jsonOAuthCC "client_secret" "SECRET_VALUE"
CkJsonObject_UpdateString $jsonOAuthCC "scope" "https://graph.microsoft.com/.default"
CkJsonObject_UpdateString $jsonOAuthCC "token_endpoint" "https://login.microsoftonline.com/TENANT_ID/oauth2/v2.0/token"
CkHttp_put_AuthToken $http [CkJsonObject_emit $jsonOAuthCC]
# --------------------------------------------------------------------------------------------------------
# Indicate that we want a JSON reply
CkHttp_put_Accept $http "application/json;odata=verbose"
CkHttp_SetUrlVar $http "sharepoint_hostname" "example.sharepoint.com"
set sbJson [new_CkStringBuilder]
set success [CkHttp_QuickGetSb $http "https://graph.microsoft.com/v1.0/sites/SITE_ID/drive/root/children" $sbJson]
if {$success == 0} then {
puts [CkHttp_lastErrorText $http]
delete_CkHttp $http
delete_CkJsonObject $jsonOAuthCC
delete_CkStringBuilder $sbJson
exit
}
set json [new_CkJsonObject]
CkJsonObject_LoadSb $json $sbJson
# Iterate over the results and get each file's name, size, and last-modified date/time.
set numFiles [CkJsonObject_SizeOfArray $json "d.results"]
puts "Number of Files in the SharePoint root folder = $numFiles"
set i 0
while {$i < $numFiles} {
CkJsonObject_put_I $json $i
set filename [CkJsonObject_stringOf $json "d.results[i].Name"]
set fileRelativeUri [CkJsonObject_stringOf $json "d.results[i].ServerRelativeUrl"]
set fileSize [CkJsonObject_IntOf $json "d.results[i].Length"]
puts [expr $i + 1]: $filename
puts " Relative URI: $fileRelativeUri"
puts " Size in Bytes: $fileSize"
set i [expr $i + 1]
}
# The output of this program when I tested it:
delete_CkHttp $http
delete_CkJsonObject $jsonOAuthCC
delete_CkStringBuilder $sbJson
delete_CkJsonObject $json