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.
Chilkat Delphi DLL Downloads
var
success: Boolean;
token: HCkJsonObject;
http: HCkHttp;
resp: HCkHttpResponse;
result: HCkJsonObject;
driveId: PWideChar;
itemId: PWideChar;
endpoint: HCkStringBuilder;
checkpointPath: PWideChar;
fac: HCkFileAccess;
checkpoint: HCkJsonObject;
url: PWideChar;
nextLink: HCkStringBuilder;
seenLinks: HCkHashtable;
pageCount: Integer;
i: Integer;
nextType: Integer;
begin
success := False;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
success := False;
// Obtain a delegated Graph token with this example's file permissions.
// See OneDrive OAuth2 with PKCE.
token := CkJsonObject_Create();
CkJsonObject_putStrict(token,True);
success := CkJsonObject_LoadFile(token,'qa_data/tokens/oneDrive.json');
if (success <> True) then
begin
Memo1.Lines.Add('Could not load the private OneDrive token cache.');
Exit;
end;
if ((CkJsonObject_JsonTypeOf(token,'access_token') <> 1) or (CkJsonObject_StringOfEquals(token,'access_token','',True) = True)) then
begin
Memo1.Lines.Add('Expected a nonempty access_token in the JSON.');
Exit;
end;
http := CkHttp_Create();
CkHttp_putAuthToken(http,CkJsonObject__stringOf(token,'access_token'));
CkHttp_putRequireSslCertVerify(http,True);
CkHttp_putFollowRedirects(http,False);
CkHttp_putConnectTimeout(http,20);
CkHttp_putReadTimeout(http,120);
CkHttp_SetRequestHeader(http,'Accept','application/json');
resp := CkHttpResponse_Create();
result := CkJsonObject_Create();
CkJsonObject_putStrict(result,True);
driveId := 'YOUR_DRIVE_ID';
itemId := 'YOUR_ITEM_ID';
endpoint := CkStringBuilder_Create();
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.
checkpointPath := 'qa_data/oneDrive/list-checkpoint.json';
fac := CkFileAccess_Create();
success := CkFileAccess_DirEnsureExists(fac,'qa_data/oneDrive');
if (success <> True) then
begin
Memo1.Lines.Add('Could not create the private checkpoint directory.');
Exit;
end;
checkpoint := CkJsonObject_Create();
CkJsonObject_putStrict(checkpoint,True);
url := CkStringBuilder__getAsString(endpoint);
if (CkFileAccess_FileExists(fac,checkpointPath) = True) then
begin
success := CkJsonObject_LoadFile(checkpoint,checkpointPath);
if (success <> True) then
begin
Memo1.Lines.Add('Invalid checkpoint; inspect it before restarting.');
Exit;
end;
if ((CkJsonObject_StringOfEquals(checkpoint,'initialUrl',url,True) <> True)) then
begin
Memo1.Lines.Add('Checkpoint belongs to another listing; no request sent.');
Exit;
end;
if (CkJsonObject_BoolOf(checkpoint,'complete') = True) then
begin
Memo1.Lines.Add('Listing already completed. Remove its checkpoint deliberately for a new scan.');
Exit;
end;
if ((CkJsonObject_JsonTypeOf(checkpoint,'nextLink') <> 1) or (CkJsonObject_StringOfEquals(checkpoint,'nextLink','',True) = True)) then
begin
Memo1.Lines.Add('Expected a nonempty nextLink in the JSON.');
Exit;
end;
url := CkJsonObject__stringOf(checkpoint,'nextLink');
end;
nextLink := CkStringBuilder_Create();
seenLinks := CkHashtable_Create();
pageCount := 0;
while ((url <> '') and (pageCount < 100)) do
begin
CkStringBuilder_SetString(nextLink,url);
if (CkStringBuilder_StartsWith(nextLink,'https://graph.microsoft.com/v1.0/',True) <> True) then
begin
Memo1.Lines.Add('Unexpected pagination host or API version; no request sent.');
Exit;
end;
if (CkHashtable_Contains(seenLinks,url) = True) then
begin
Memo1.Lines.Add('Repeated pagination URL; stopping.');
Exit;
end;
CkHashtable_AddInt(seenLinks,url,1);
success := CkHttp_HttpNoBody(http,'GET',url,resp);
if ((success <> True) and (CkHttpResponse_getStatusCode(resp) = 0)) then
begin
Memo1.Lines.Add('No HTTP response received; inspect diagnostics privately and reconcile writes before retrying.');
Exit;
end;
if ((CkHttpResponse_getStatusCode(resp) <> 200)) then
begin
Memo1.Lines.Add('Unexpected HTTP status: ' + IntToStr(CkHttpResponse_getStatusCode(resp)));
Memo1.Lines.Add('Request ID: ' + CkHttpResponse__getHeaderField(resp,'request-id'));
if (CkHttpResponse_getStatusCode(resp) = 429) then
begin
Memo1.Lines.Add('Retry-After: ' + CkHttpResponse__getHeaderField(resp,'Retry-After'));
end;
Exit;
end;
success := CkJsonObject_Load(result,CkHttpResponse__bodyStr(resp));
if (success <> True) then
begin
Memo1.Lines.Add('Expected a well-formed JSON response.');
Exit;
end;
if (CkJsonObject_JsonTypeOf(result,'value') <> 4) then
begin
Memo1.Lines.Add('Expected a value array.');
Exit;
end;
i := 0;
while (i < CkJsonObject_SizeOfArray(result,'value')) do
begin
CkJsonObject_putI(result,i);
if ((CkJsonObject_JsonTypeOf(result,'value[i].id') <> 1) or (CkJsonObject_StringOfEquals(result,'value[i].id','',True) = True)) then
begin
Memo1.Lines.Add('Expected a nonempty value[i].id in the JSON.');
Exit;
end;
Memo1.Lines.Add('Item ID: ' + CkJsonObject__stringOf(result,'value[i].id'));
Memo1.Lines.Add('Name: ' + CkJsonObject__stringOf(result,'value[i].name'));
// Size can exceed a 32-bit integer; keep the JSON number's decimal text.
Memo1.Lines.Add('Size in bytes: ' + CkJsonObject__stringOf(result,'value[i].size'));
if (CkJsonObject_JsonTypeOf(result,'value[i].folder') = 3) then
begin
Memo1.Lines.Add('Folder; children: ' + CkJsonObject__stringOf(result,'value[i].folder.childCount'));
end;
if (CkJsonObject_JsonTypeOf(result,'value[i].file') = 3) then
begin
Memo1.Lines.Add('File type: ' + CkJsonObject__stringOf(result,'value[i].file.mimeType'));
end;
if (CkJsonObject_JsonTypeOf(result,'value[i].remoteItem') = 3) then
begin
Memo1.Lines.Add('Remote drive ID: ' + CkJsonObject__stringOf(result,'value[i].remoteItem.parentReference.driveId'));
Memo1.Lines.Add('Remote item ID: ' + CkJsonObject__stringOf(result,'value[i].remoteItem.id'));
end;
i := i + 1;
end;
pageCount := pageCount + 1;
// A quoted JSON path treats the period in @odata.nextLink literally.
nextType := CkJsonObject_JsonTypeOf(result,'"@odata.nextLink"');
if ((nextType = -1) or (nextType = 6)) then
begin
url := '';
end
else
begin
if ((nextType <> 1) or (CkJsonObject_StringOfEquals(result,'"@odata.nextLink"','',True) = True)) then
begin
Memo1.Lines.Add('Invalid continuation link; cannot establish completeness.');
Exit;
end;
url := CkJsonObject__stringOf(result,'"@odata.nextLink"');
end;
// 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 = '');
success := CkJsonObject_WriteFile(checkpoint,checkpointPath);
if (success <> True) then
begin
Memo1.Lines.Add('Could not save progress; the current page may repeat.');
Exit;
end;
end;
if (url <> '') then
begin
Memo1.Lines.Add('Stopped at the example''s 100-page limit; run again to resume its saved continuation.');
Exit;
end;
Memo1.Lines.Add('Completed pages: ' + IntToStr(pageCount));
CkJsonObject_Dispose(token);
CkHttp_Dispose(http);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(result);
CkStringBuilder_Dispose(endpoint);
CkFileAccess_Dispose(fac);
CkJsonObject_Dispose(checkpoint);
CkStringBuilder_Dispose(nextLink);
CkHashtable_Dispose(seenLinks);