Sample code for 30+ languages & platforms
Tcl Requires Chilkat v11.6.0+

OneDrive: Get File or Folder Metadata by Path

See more OneDrive Examples

Retrieve a known Documents/sample.txt path without first listing folders. Select useful fields and inspect optional facets. eTag is an opaque version tag for conditional requests; retain it exactly, including quotes. cTag, image, photo, and hash fields are not guaranteed for every item/account. Metadata is not file content. This example intentionally does not request or print a preauthenticated download URL.

Keep drive IDs and item IDs as opaque strings, retaining the drive context with each item. Encode individual path segments, not an entire URL or a slash-separated path in one call. An item ID remains stable across rename/move within its drive; a name-based path changes. OneDrive does not use Outlook's ImmutableId preference.

Permission: Files.Read.

Uses direct Chilkat.Http requests to Microsoft Graph v1.0 in the global cloud. The delegated token is read from the private qa_data/tokens/oneDrive.json file created by the PKCE authorization example. Personal Microsoft accounts and Microsoft 365 work/school accounts can use delegated authorization when the app registration supports them. Refresh expired tokens; scopes and actual file access both matter.

For app-only Microsoft 365 access, use client credentials with administrator-approved application permissions. /me requires a signed-in user; app-only calls must address an authorized user, site, or drive explicitly. Client credentials do not grant access to personal OneDrive accounts. National clouds use their own authority and Graph hosts.

Check the expected HTTP status before using a response body. A false method result can still contain an HTTP error response; status zero means no response arrived. For 401, renew authorization; for 403, check permissions, access grants, and policy; for 429, honor Retry-After with a bounded retry. Reconcile uncertain writes before repeating them. Keep credentials, file contents, upload/download URLs, and detailed diagnostics private.

Microsoft documentation

Chilkat Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

#  Assumes Chilkat is unlocked. See Global Unlock Sample.
set success 0
#  Obtain a delegated Graph token with this example's file permissions.
#  See OneDrive OAuth2 with PKCE.
set token [new_CkJsonObject]

CkJsonObject_put_Strict $token 1
set success [CkJsonObject_LoadFile $token "qa_data/tokens/oneDrive.json"]
if {$success != 1} then {
    puts "Could not load the private OneDrive token cache."
    delete_CkJsonObject $token
    exit
}

if {expr [[CkJsonObject_JsonTypeOf $token "access_token"] != 1]  ||  [[CkJsonObject_StringOfEquals $token "access_token" "" 1] == 1]} then {
    puts "Expected a nonempty access_token in the JSON."
    delete_CkJsonObject $token
    exit
}

set http [new_CkHttp]

CkHttp_put_AuthToken $http [CkJsonObject_stringOf $token "access_token"]
CkHttp_put_RequireSslCertVerify $http 1
CkHttp_put_FollowRedirects $http 0
CkHttp_put_ConnectTimeout $http 20
CkHttp_put_ReadTimeout $http 120
CkHttp_SetRequestHeader $http "Accept" "application/json"
set resp [new_CkHttpResponse]

set result [new_CkJsonObject]

CkJsonObject_put_Strict $result 1
set driveId "YOUR_DRIVE_ID"
set folderName "Documents"
set fileName "sample.txt"
set endpoint [new_CkStringBuilder]

CkStringBuilder_Append $endpoint "https://graph.microsoft.com/v1.0/drives/"
CkStringBuilder_Append $endpoint [CkHttp_urlEncode $http $driveId]
CkStringBuilder_Append $endpoint "/root:/"
CkStringBuilder_Append $endpoint [CkHttp_urlEncode $http $folderName]
CkStringBuilder_Append $endpoint "/"
CkStringBuilder_Append $endpoint [CkHttp_urlEncode $http $fileName]
CkStringBuilder_Append $endpoint "?$select=id,name,size,eTag,cTag,lastModifiedDateTime,parentReference,file,folder,image,photo"
set success [CkHttp_HttpNoBody $http "GET" [CkStringBuilder_getAsString $endpoint] $resp]
if {expr [$success != 1]  &&  [[CkHttpResponse_get_StatusCode $resp] == 0]} then {
    puts "No HTTP response received; inspect diagnostics privately and reconcile writes before retrying."
    delete_CkJsonObject $token
    delete_CkHttp $http
    delete_CkHttpResponse $resp
    delete_CkJsonObject $result
    delete_CkStringBuilder $endpoint
    exit
}

if {[CkHttpResponse_get_StatusCode $resp] != 200} then {
    puts "Unexpected HTTP status: [CkHttpResponse_get_StatusCode $resp]"
    puts "Request ID: [CkHttpResponse_getHeaderField $resp request-id]"
    if {[CkHttpResponse_get_StatusCode $resp] == 429} then {
        puts "Retry-After: [CkHttpResponse_getHeaderField $resp Retry-After]"
    }

    delete_CkJsonObject $token
    delete_CkHttp $http
    delete_CkHttpResponse $resp
    delete_CkJsonObject $result
    delete_CkStringBuilder $endpoint
    exit
}

set success [CkJsonObject_Load $result [CkHttpResponse_bodyStr $resp]]
if {$success != 1} then {
    puts "Expected a well-formed JSON response."
    delete_CkJsonObject $token
    delete_CkHttp $http
    delete_CkHttpResponse $resp
    delete_CkJsonObject $result
    delete_CkStringBuilder $endpoint
    exit
}

if {expr [[CkJsonObject_JsonTypeOf $result "id"] != 1]  ||  [[CkJsonObject_StringOfEquals $result "id" "" 1] == 1]} then {
    puts "Expected a nonempty id in the JSON."
    delete_CkJsonObject $token
    delete_CkHttp $http
    delete_CkHttpResponse $resp
    delete_CkJsonObject $result
    delete_CkStringBuilder $endpoint
    exit
}

puts "Item ID: [CkJsonObject_stringOf $result id]"
puts "Name: [CkJsonObject_stringOf $result name]"
puts "Size in bytes: [CkJsonObject_stringOf $result size]"
puts "ETag: [CkJsonObject_stringOf $result eTag]"
puts "Parent ID: [CkJsonObject_stringOf $result parentReference.id]"
puts "Modified (UTC timestamp): [CkJsonObject_stringOf $result lastModifiedDateTime]"
if {[CkJsonObject_JsonTypeOf $result "file"] == 3} then {
    puts "File MIME type: [CkJsonObject_stringOf $result file.mimeType]"
}

if {[CkJsonObject_JsonTypeOf $result "folder"] == 3} then {
    puts "Child count: [CkJsonObject_stringOf $result folder.childCount]"
}

if {[CkJsonObject_JsonTypeOf $result "image"] == 3} then {
    puts "Image width: [CkJsonObject_IntOf $result image.width]"
    puts "Image height: [CkJsonObject_IntOf $result image.height]"
}

if {[CkJsonObject_JsonTypeOf $result "photo.takenDateTime"] == 1} then {
    puts "Photo timestamp: [CkJsonObject_stringOf $result photo.takenDateTime]"
}


delete_CkJsonObject $token
delete_CkHttp $http
delete_CkHttpResponse $resp
delete_CkJsonObject $result
delete_CkStringBuilder $endpoint