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

OneDrive: Resume a Paginated Folder Listing

See more OneDrive Examples

List a folder by ID and save progress after each processed page. Run again to continue from the complete nextLink, or deliberately remove the completed checkpoint to start a new scan. The checkpoint is tied to the initial drive, folder, and projection URL; keep a private account-specific path. This example prints records; production applications must persist processed data before the cursor, atomically where possible, and tolerate repeated items.

Pagination is not synchronization and can be affected by concurrent changes. Use delta for a maintained local view. If a continuation expires, reconcile and restart the scan. An incomplete checkpoint write can require recovery; use atomic replacement in production.

Follows complete @odata.nextLink URLs, including after empty pages. It validates the Graph host/API path, rejects repeated links, and reports a stop at the demonstration bound of 100 pages. $top is page size, not the total result count. Enumeration is not a transactionally consistent snapshot during concurrent changes.

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 itemId "YOUR_ITEM_ID"
set endpoint [new_CkStringBuilder]

CkStringBuilder_Append $endpoint "https://graph.microsoft.com/v1.0/drives/"
CkStringBuilder_Append $endpoint [CkHttp_urlEncode $http $driveId]
CkStringBuilder_Append $endpoint "/items/"
CkStringBuilder_Append $endpoint [CkHttp_urlEncode $http $itemId]
CkStringBuilder_Append $endpoint "/children?$select=id,name,size,file,folder,package,remoteItem,parentReference,lastModifiedDateTime,eTag&$top=25"
#  Use a separate checkpoint for each account, drive, folder, and projection.
set checkpointPath "qa_data/oneDrive/list-checkpoint.json"
set fac [new_CkFileAccess]

set success [CkFileAccess_DirEnsureExists $fac "qa_data/oneDrive"]
if {$success != 1} then {
    puts "Could not create the private checkpoint directory."
    delete_CkJsonObject $token
    delete_CkHttp $http
    delete_CkHttpResponse $resp
    delete_CkJsonObject $result
    delete_CkStringBuilder $endpoint
    delete_CkFileAccess $fac
    exit
}

set checkpoint [new_CkJsonObject]

CkJsonObject_put_Strict $checkpoint 1
set url [CkStringBuilder_getAsString $endpoint]
if {[CkFileAccess_FileExists $fac $checkpointPath] == 1} then {
    set success [CkJsonObject_LoadFile $checkpoint $checkpointPath]
    if {$success != 1} then {
        puts "Invalid checkpoint; inspect it before restarting."
        delete_CkJsonObject $token
        delete_CkHttp $http
        delete_CkHttpResponse $resp
        delete_CkJsonObject $result
        delete_CkStringBuilder $endpoint
        delete_CkFileAccess $fac
        delete_CkJsonObject $checkpoint
        exit
    }

    if {[CkJsonObject_StringOfEquals $checkpoint "initialUrl" $url 1] != 1} then {
        puts "Checkpoint belongs to another listing; no request sent."
        delete_CkJsonObject $token
        delete_CkHttp $http
        delete_CkHttpResponse $resp
        delete_CkJsonObject $result
        delete_CkStringBuilder $endpoint
        delete_CkFileAccess $fac
        delete_CkJsonObject $checkpoint
        exit
    }

    if {[CkJsonObject_BoolOf $checkpoint "complete"] == 1} then {
        puts "Listing already completed. Remove its checkpoint deliberately for a new scan."
        delete_CkJsonObject $token
        delete_CkHttp $http
        delete_CkHttpResponse $resp
        delete_CkJsonObject $result
        delete_CkStringBuilder $endpoint
        delete_CkFileAccess $fac
        delete_CkJsonObject $checkpoint
        exit
    }

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

    set url [CkJsonObject_stringOf $checkpoint "nextLink"]
}

set nextLink [new_CkStringBuilder]

set seenLinks [new_CkHashtable]

set pageCount 0
while {expr [$url != ""]  &&  [$pageCount < 100]} {
    CkStringBuilder_SetString $nextLink $url
    if {[CkStringBuilder_StartsWith $nextLink "https://graph.microsoft.com/v1.0/" 1] != 1} then {
        puts "Unexpected pagination host or API version; no request sent."
        delete_CkJsonObject $token
        delete_CkHttp $http
        delete_CkHttpResponse $resp
        delete_CkJsonObject $result
        delete_CkStringBuilder $endpoint
        delete_CkFileAccess $fac
        delete_CkJsonObject $checkpoint
        delete_CkStringBuilder $nextLink
        delete_CkHashtable $seenLinks
        exit
    }

    if {[CkHashtable_Contains $seenLinks $url] == 1} then {
        puts "Repeated pagination URL; stopping."
        delete_CkJsonObject $token
        delete_CkHttp $http
        delete_CkHttpResponse $resp
        delete_CkJsonObject $result
        delete_CkStringBuilder $endpoint
        delete_CkFileAccess $fac
        delete_CkJsonObject $checkpoint
        delete_CkStringBuilder $nextLink
        delete_CkHashtable $seenLinks
        exit
    }

    CkHashtable_AddInt $seenLinks $url 1
    set success [CkHttp_HttpNoBody $http "GET" $url $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
        delete_CkFileAccess $fac
        delete_CkJsonObject $checkpoint
        delete_CkStringBuilder $nextLink
        delete_CkHashtable $seenLinks
        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
        delete_CkFileAccess $fac
        delete_CkJsonObject $checkpoint
        delete_CkStringBuilder $nextLink
        delete_CkHashtable $seenLinks
        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
        delete_CkFileAccess $fac
        delete_CkJsonObject $checkpoint
        delete_CkStringBuilder $nextLink
        delete_CkHashtable $seenLinks
        exit
    }

    if {[CkJsonObject_JsonTypeOf $result "value"] != 4} then {
        puts "Expected a value array."
        delete_CkJsonObject $token
        delete_CkHttp $http
        delete_CkHttpResponse $resp
        delete_CkJsonObject $result
        delete_CkStringBuilder $endpoint
        delete_CkFileAccess $fac
        delete_CkJsonObject $checkpoint
        delete_CkStringBuilder $nextLink
        delete_CkHashtable $seenLinks
        exit
    }

    set i 0
    while {$i < [CkJsonObject_SizeOfArray $result "value"]} {
        CkJsonObject_put_I $result $i
        if {expr [[CkJsonObject_JsonTypeOf $result "value[i].id"] != 1]  ||  [[CkJsonObject_StringOfEquals $result "value[i].id" "" 1] == 1]} then {
            puts "{Expected a nonempty value[i].id in the JSON.}"
            delete_CkJsonObject $token
            delete_CkHttp $http
            delete_CkHttpResponse $resp
            delete_CkJsonObject $result
            delete_CkStringBuilder $endpoint
            delete_CkFileAccess $fac
            delete_CkJsonObject $checkpoint
            delete_CkStringBuilder $nextLink
            delete_CkHashtable $seenLinks
            exit
        }

        puts "Item ID: [CkJsonObject_stringOf $result {value[i].id}]"
        puts "Name: [CkJsonObject_stringOf $result {value[i].name}]"
        #  Size can exceed a 32-bit integer; keep the JSON number's decimal text.
        puts "Size in bytes: [CkJsonObject_stringOf $result {value[i].size}]"
        if {[CkJsonObject_JsonTypeOf $result "value[i].folder"] == 3} then {
            puts "Folder; children: [CkJsonObject_stringOf $result {value[i].folder.childCount}]"
        }

        if {[CkJsonObject_JsonTypeOf $result "value[i].file"] == 3} then {
            puts "File type: [CkJsonObject_stringOf $result {value[i].file.mimeType}]"
        }

        if {[CkJsonObject_JsonTypeOf $result "value[i].remoteItem"] == 3} then {
            puts "Remote drive ID: [CkJsonObject_stringOf $result {value[i].remoteItem.parentReference.driveId}]"
            puts "Remote item ID: [CkJsonObject_stringOf $result {value[i].remoteItem.id}]"
        }

        set i [expr $i + 1]
    }
    set pageCount [expr $pageCount + 1]
    #  A quoted JSON path treats the period in @odata.nextLink literally.
    set nextType [CkJsonObject_JsonTypeOf $result "\"@odata.nextLink\""]
    if {expr [$nextType == -1]  ||  [$nextType == 6]} then {
        set url ""
    }     else {
        if {expr [$nextType != 1]  ||  [[CkJsonObject_StringOfEquals $result "\"@odata.nextLink\"" "" 1] == 1]} then {
            puts "Invalid continuation link; cannot establish completeness."
            delete_CkJsonObject $token
            delete_CkHttp $http
            delete_CkHttpResponse $resp
            delete_CkJsonObject $result
            delete_CkStringBuilder $endpoint
            delete_CkFileAccess $fac
            delete_CkJsonObject $checkpoint
            delete_CkStringBuilder $nextLink
            delete_CkHashtable $seenLinks
            exit
        }

        set url [CkJsonObject_stringOf $result "\"@odata.nextLink\""]
    }

    #  Commit the application's processed records before advancing this checkpoint.
    CkJsonObject_UpdateString $checkpoint "initialUrl" [CkStringBuilder_getAsString $endpoint]
    CkJsonObject_UpdateString $checkpoint "nextLink" $url
    CkJsonObject_UpdateBool $checkpoint "complete" [$url == ""]
    set success [CkJsonObject_WriteFile $checkpoint $checkpointPath]
    if {$success != 1} then {
        puts "Could not save progress; the current page may repeat."
        delete_CkJsonObject $token
        delete_CkHttp $http
        delete_CkHttpResponse $resp
        delete_CkJsonObject $result
        delete_CkStringBuilder $endpoint
        delete_CkFileAccess $fac
        delete_CkJsonObject $checkpoint
        delete_CkStringBuilder $nextLink
        delete_CkHashtable $seenLinks
        exit
    }

}
if {$url != ""} then {
    puts "Stopped at the example's 100-page limit; run again to resume its saved continuation."
    delete_CkJsonObject $token
    delete_CkHttp $http
    delete_CkHttpResponse $resp
    delete_CkJsonObject $result
    delete_CkStringBuilder $endpoint
    delete_CkFileAccess $fac
    delete_CkJsonObject $checkpoint
    delete_CkStringBuilder $nextLink
    delete_CkHashtable $seenLinks
    exit
}

puts "Completed pages: $pageCount"

delete_CkJsonObject $token
delete_CkHttp $http
delete_CkHttpResponse $resp
delete_CkJsonObject $result
delete_CkStringBuilder $endpoint
delete_CkFileAccess $fac
delete_CkJsonObject $checkpoint
delete_CkStringBuilder $nextLink
delete_CkHashtable $seenLinks