OneDrive: List Versions and Download a Previous Version
See more OneDrive Examples
Enumerate a file's version history, then optionally download an explicitly selected older version. The default only lists. Keep version IDs such as 1.0 as strings and do not infer the latest version by numeric sorting. Available history and fields depend on the drive and retention settings. The version-content API cannot download the current version; use the ordinary file-content download example for that.
Accepts direct content or the documented redirect response. Binary bytes, including empty files, are saved to a generated local filename only after HTTP success. This demonstration buffers the version in memory; use streamed download handling for large files. Downloading a version does not restore or change the current file.
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.
Microsoft has announced changes to preauthenticated URLs beginning April 1, 2027. The example supports Graph-hosted URLs with the Graph token and an explicitly configured SharePoint host with a separate SharePoint-audience token. A legacy preauthenticated URL is requested without a bearer token. Never forward the Graph token to a SharePoint or content host. If required, prepare private qa_data/tokens/oneDrive-sharepoint.json with host and access_token from a separately authorized SharePoint token acquisition; token refresh must preserve that audience. A 401 requires checking the correct token audience or URL expiry, not blindly adding credentials.
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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoToken
Handle hoHttp
Variant vResp
Handle hoResp
Handle hoResult
String sDriveId
String sItemId
Handle hoEndpoint
String sVersionId
Boolean iFoundVersion
String sUrl
Handle hoNextLink
Handle hoSeenLinks
Integer iPageCount
Integer i
Integer iNextType
Variant vContents
Handle hoContents
String sDownloadUrl
Handle hoServiceUrl
Handle hoDownloadHttp
Handle hoServiceHost
Files Handle hoTokenFiles
Handle hoSharepointToken
Variant vDownloadResponse
Handle hoDownloadResponse
Handle hoFac
Handle hoUuid
String sLocalId
Handle hoTarget
String sTemp1
String sTemp2
String sTemp3
Integer iTemp1
Integer iTemp2
Boolean bTemp1
Move False To iSuccess
// Assumes Chilkat is unlocked. See Global Unlock Sample.
Move False To iSuccess
// Obtain a delegated Graph token with this example's file permissions.
// See OneDrive OAuth2 with PKCE.
Get Create (RefClass(cComChilkatJsonObject)) To hoToken
If (Not(IsComObjectCreated(hoToken))) Begin
Send CreateComObject of hoToken
End
Set ComStrict Of hoToken To True
Get ComLoadFile Of hoToken "qa_data/tokens/oneDrive.json" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not load the private OneDrive token cache."
Procedure_Return
End
Get ComJsonTypeOf Of hoToken "access_token" To iTemp1
Get ComStringOfEquals Of hoToken "access_token" "" True To bTemp1
If ((iTemp1 <> 1) Or (bTemp1 = True)) Begin
Showln "Expected a nonempty access_token in the JSON."
Procedure_Return
End
Get Create (RefClass(cComChilkatHttp)) To hoHttp
If (Not(IsComObjectCreated(hoHttp))) Begin
Send CreateComObject of hoHttp
End
Get ComStringOf Of hoToken "access_token" To sTemp1
Set ComAuthToken Of hoHttp To sTemp1
Set ComRequireSslCertVerify Of hoHttp To True
Set ComFollowRedirects Of hoHttp To False
Set ComConnectTimeout Of hoHttp To 20
Set ComReadTimeout Of hoHttp To 120
Send ComSetRequestHeader To hoHttp "Accept" "application/json"
Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
If (Not(IsComObjectCreated(hoResp))) Begin
Send CreateComObject of hoResp
End
Get Create (RefClass(cComChilkatJsonObject)) To hoResult
If (Not(IsComObjectCreated(hoResult))) Begin
Send CreateComObject of hoResult
End
Set ComStrict Of hoResult To True
Move "YOUR_DRIVE_ID" To sDriveId
Move "YOUR_ITEM_ID" To sItemId
Get Create (RefClass(cComChilkatStringBuilder)) To hoEndpoint
If (Not(IsComObjectCreated(hoEndpoint))) Begin
Send CreateComObject of hoEndpoint
End
Get ComAppend Of hoEndpoint "https://graph.microsoft.com/v1.0/drives/" To iSuccess
Get ComUrlEncode Of hoHttp sDriveId To sTemp1
Get ComAppend Of hoEndpoint sTemp1 To iSuccess
Get ComAppend Of hoEndpoint "/items/" To iSuccess
Get ComUrlEncode Of hoHttp sItemId To sTemp1
Get ComAppend Of hoEndpoint sTemp1 To iSuccess
// Leave empty to list only. Set an explicit previous version ID to download it.
// A version ID such as 1.0 is a STRING, not a decimal number.
Move "" To sVersionId
Move False To iFoundVersion
Get ComAppend Of hoEndpoint "/versions" To iSuccess
Get ComGetAsString Of hoEndpoint To sUrl
Get Create (RefClass(cComChilkatStringBuilder)) To hoNextLink
If (Not(IsComObjectCreated(hoNextLink))) Begin
Send CreateComObject of hoNextLink
End
Get Create (RefClass(cComChilkatHashtable)) To hoSeenLinks
If (Not(IsComObjectCreated(hoSeenLinks))) Begin
Send CreateComObject of hoSeenLinks
End
Move 0 To iPageCount
While ((sUrl <> "") And (iPageCount < 100))
Get ComSetString Of hoNextLink sUrl To iSuccess
Get ComStartsWith Of hoNextLink "https://graph.microsoft.com/v1.0/" True To bTemp1
If (bTemp1 <> True) Begin
Showln "Unexpected pagination host or API version; no request sent."
Procedure_Return
End
Get ComContains Of hoSeenLinks sUrl To bTemp1
If (bTemp1 = True) Begin
Showln "Repeated pagination URL; stopping."
Procedure_Return
End
Get ComAddInt Of hoSeenLinks sUrl 1 To iSuccess
Get pvComObject of hoResp to vResp
Get ComHttpNoBody Of hoHttp "GET" sUrl vResp To iSuccess
Get ComStatusCode Of hoResp To iTemp1
If ((iSuccess <> True) And (iTemp1 = 0)) Begin
Showln "No HTTP response received; inspect diagnostics privately and reconcile writes before retrying."
Procedure_Return
End
Get ComStatusCode Of hoResp To iTemp1
If ((iTemp1 <> 200)) Begin
Get ComStatusCode Of hoResp To iTemp1
Showln "Unexpected HTTP status: " iTemp1
Get ComGetHeaderField Of hoResp "request-id" To sTemp1
Showln "Request ID: " sTemp1
Get ComStatusCode Of hoResp To iTemp1
If (iTemp1 = 429) Begin
Get ComGetHeaderField Of hoResp "Retry-After" To sTemp1
Showln "Retry-After: " sTemp1
End
Procedure_Return
End
Get ComBodyStr Of hoResp To sTemp1
Get ComLoad Of hoResult sTemp1 To iSuccess
If (iSuccess <> True) Begin
Showln "Expected a well-formed JSON response."
Procedure_Return
End
Get ComJsonTypeOf Of hoResult "value" To iTemp1
If (iTemp1 <> 4) Begin
Showln "Expected a value array."
Procedure_Return
End
Move 0 To i
While (i < (ComSizeOfArray(hoResult, "value")))
Set ComI Of hoResult To i
Get ComJsonTypeOf Of hoResult "value[i].id" To iTemp1
Get ComStringOfEquals Of hoResult "value[i].id" "" True To bTemp1
If ((iTemp1 <> 1) Or (bTemp1 = True)) Begin
Showln "Expected a nonempty value[i].id in the JSON."
Procedure_Return
End
Get ComStringOf Of hoResult "value[i].id" To sTemp1
Showln "Version ID: " sTemp1
Get ComStringOf Of hoResult "value[i].lastModifiedDateTime" To sTemp1
Showln "Modified: " sTemp1
Get ComStringOf Of hoResult "value[i].size" To sTemp1
Showln "Size in bytes: " sTemp1
Get ComStringOfEquals Of hoResult "value[i].id" sVersionId True To bTemp1
If (bTemp1 = True) Begin
Move True To iFoundVersion
End
Move (i + 1) To i
Loop
Move (iPageCount + 1) To iPageCount
// A quoted JSON path treats the period in @odata.nextLink literally.
Get ComJsonTypeOf Of hoResult '"@odata.nextLink"' To iNextType
If ((iNextType = -1) Or (iNextType = 6)) Begin
Move "" To sUrl
End
Else Begin
Get ComStringOfEquals Of hoResult '"@odata.nextLink"' "" True To bTemp1
If ((iNextType <> 1) Or (bTemp1 = True)) Begin
Showln "Invalid continuation link; cannot establish completeness."
Procedure_Return
End
Get ComStringOf Of hoResult '"@odata.nextLink"' To sUrl
End
Loop
If (sUrl <> "") Begin
Showln "Stopped at the example's 100-page limit; save the continuation privately to resume."
Procedure_Return
End
Showln "Completed pages: " iPageCount
If (sVersionId = "") Begin
Showln "Listing only. Select a previous version ID to download."
Procedure_Return
End
If (iFoundVersion <> True) Begin
Showln "The selected version was not in the completed listing; nothing downloaded."
Procedure_Return
End
Get ComAppend Of hoEndpoint "/" To iSuccess
Get ComUrlEncode Of hoHttp sVersionId To sTemp1
Get ComAppend Of hoEndpoint sTemp1 To iSuccess
Get ComAppend Of hoEndpoint "/content" To iSuccess
Send ComSetRequestHeader To hoHttp "Accept" "*/*"
Get ComGetAsString Of hoEndpoint To sTemp1
Get pvComObject of hoResp to vResp
Get ComHttpNoBody Of hoHttp "GET" sTemp1 vResp To iSuccess
Get ComStatusCode Of hoResp To iTemp1
If ((iSuccess <> True) And (iTemp1 = 0)) Begin
Showln "No HTTP response received; inspect diagnostics privately and reconcile writes before retrying."
Procedure_Return
End
Get ComStatusCode Of hoResp To iTemp1
Get ComStatusCode Of hoResp To iTemp2
If ((iTemp1 <> 200) And (iTemp2 <> 302)) Begin
Get ComStatusCode Of hoResp To iTemp1
Showln "Unexpected HTTP status: " iTemp1
Get ComGetHeaderField Of hoResp "request-id" To sTemp1
Showln "Request ID: " sTemp1
Get ComStatusCode Of hoResp To iTemp1
If (iTemp1 = 429) Begin
Get ComGetHeaderField Of hoResp "Retry-After" To sTemp1
Showln "Retry-After: " sTemp1
End
Procedure_Return
End
Get Create (RefClass(cComChilkatBinData)) To hoContents
If (Not(IsComObjectCreated(hoContents))) Begin
Send CreateComObject of hoContents
End
Get ComStatusCode Of hoResp To iTemp1
If (iTemp1 = 200) Begin
Get pvComObject of hoContents to vContents
Get ComGetBodyBd Of hoResp vContents To iSuccess
If (iSuccess <> True) Begin
Showln "Could not read the version bytes."
Procedure_Return
End
End
Else Begin
Get ComGetHeaderField Of hoResp "Location" To sDownloadUrl
Get Create (RefClass(cComChilkatUrl)) To hoServiceUrl
If (Not(IsComObjectCreated(hoServiceUrl))) Begin
Send CreateComObject of hoServiceUrl
End
Get ComParseUrl Of hoServiceUrl sDownloadUrl To iSuccess
If (iSuccess <> True) Begin
Showln "Invalid service URL."
Procedure_Return
End
Get ComSsl Of hoServiceUrl To bTemp1
Get ComHost Of hoServiceUrl To sTemp1
Get ComLogin Of hoServiceUrl To sTemp2
Get ComPassword Of hoServiceUrl To sTemp3
If ((bTemp1 <> True) Or (sTemp1 = "") Or (sTemp2 <> "") Or (sTemp3 <> "")) Begin
Showln "Expected an HTTPS service URL without embedded login credentials."
Procedure_Return
End
Get Create (RefClass(cComChilkatHttp)) To hoDownloadHttp
If (Not(IsComObjectCreated(hoDownloadHttp))) Begin
Send CreateComObject of hoDownloadHttp
End
Set ComRequireSslCertVerify Of hoDownloadHttp To True
Set ComFollowRedirects Of hoDownloadHttp To False
Set ComConnectTimeout Of hoDownloadHttp To 20
Set ComReadTimeout Of hoDownloadHttp To 120
Get Create (RefClass(cComChilkatStringBuilder)) To hoServiceHost
If (Not(IsComObjectCreated(hoServiceHost))) Begin
Send CreateComObject of hoServiceHost
End
Get ComHost Of hoServiceUrl To sTemp1
Get ComAppend Of hoServiceHost sTemp1 To iSuccess
Get ComContentsEqual Of hoServiceHost "graph.microsoft.com" False To bTemp1
Get ComPort Of hoServiceUrl To iTemp1
If ((bTemp1 = True) And (iTemp1 = 443)) Begin
Get ComStringOf Of hoToken "access_token" To sTemp1
Set ComAuthToken Of hoDownloadHttp To sTemp1
End
Else Begin
Get Create (RefClass(cComCkFileAccess)) To hoTokenFiles
If (Not(IsComObjectCreated(hoTokenFiles))) Begin
Send CreateComObject of hoTokenFiles
End
Get ComFileExists Of hoTokenFiles "qa_data/tokens/oneDrive-sharepoint.json" To bTemp1
If (bTemp1 = True) Begin
Get Create (RefClass(cComChilkatJsonObject)) To hoSharepointToken
If (Not(IsComObjectCreated(hoSharepointToken))) Begin
Send CreateComObject of hoSharepointToken
End
Set ComStrict Of hoSharepointToken To True
Get ComLoadFile Of hoSharepointToken "qa_data/tokens/oneDrive-sharepoint.json" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not read the private SharePoint token cache."
Procedure_Return
End
Get ComJsonTypeOf Of hoSharepointToken "host" To iTemp1
Get ComStringOfEquals Of hoSharepointToken "host" "" True To bTemp1
If ((iTemp1 <> 1) Or (bTemp1 = True)) Begin
Showln "Expected a nonempty host in the JSON."
Procedure_Return
End
Get ComJsonTypeOf Of hoSharepointToken "access_token" To iTemp1
Get ComStringOfEquals Of hoSharepointToken "access_token" "" True To bTemp1
If ((iTemp1 <> 1) Or (bTemp1 = True)) Begin
Showln "Expected a nonempty access_token in the JSON."
Procedure_Return
End
// host is a configured exact hostname, not a URL and not obtained from the service response.
Get ComContentsEqual Of hoServiceHost (ComStringOf(hoSharepointToken, "host")) False To bTemp1
Get ComPort Of hoServiceUrl To iTemp1
If ((bTemp1 = True) And (iTemp1 = 443)) Begin
Get ComStringOf Of hoSharepointToken "access_token" To sTemp1
Set ComAuthToken Of hoDownloadHttp To sTemp1
End
End
End
// With no matching token configured, only a still-valid preauthenticated URL can succeed.
Get Create (RefClass(cComChilkatHttpResponse)) To hoDownloadResponse
If (Not(IsComObjectCreated(hoDownloadResponse))) Begin
Send CreateComObject of hoDownloadResponse
End
Get pvComObject of hoDownloadResponse to vDownloadResponse
Get ComHttpNoBody Of hoDownloadHttp "GET" sDownloadUrl vDownloadResponse To iSuccess
Get ComStatusCode Of hoDownloadResponse To iTemp1
If ((iSuccess <> True) And (iTemp1 = 0)) Begin
Showln "No HTTP response received; inspect diagnostics privately and reconcile writes before retrying."
Procedure_Return
End
Get ComStatusCode Of hoDownloadResponse To iTemp1
If ((iTemp1 <> 200)) Begin
Get ComStatusCode Of hoDownloadResponse To iTemp1
Showln "Unexpected HTTP status: " iTemp1
Get ComGetHeaderField Of hoDownloadResponse "request-id" To sTemp1
Showln "Request ID: " sTemp1
Get ComStatusCode Of hoDownloadResponse To iTemp1
If (iTemp1 = 429) Begin
Get ComGetHeaderField Of hoDownloadResponse "Retry-After" To sTemp1
Showln "Retry-After: " sTemp1
End
Procedure_Return
End
Get pvComObject of hoContents to vContents
Get ComGetBodyBd Of hoDownloadResponse vContents To iSuccess
If (iSuccess <> True) Begin
Showln "Could not read the version download."
Procedure_Return
End
End
// Hold binary bytes without text conversion. This example expects a version that fits in memory.
Get Create (RefClass(cComCkFileAccess)) To hoFac
If (Not(IsComObjectCreated(hoFac))) Begin
Send CreateComObject of hoFac
End
Get ComDirEnsureExists Of hoFac "qa_output/oneDrive" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not create the output directory."
Procedure_Return
End
Get Create (RefClass(cComChilkatCrypt2)) To hoUuid
If (Not(IsComObjectCreated(hoUuid))) Begin
Send CreateComObject of hoUuid
End
Get ComGenerateUuid Of hoUuid To sLocalId
Get ComLastMethodSuccess Of hoUuid To bTemp1
If (bTemp1 <> True) Begin
Showln "Could not generate a local filename."
Procedure_Return
End
Get Create (RefClass(cComChilkatStringBuilder)) To hoTarget
If (Not(IsComObjectCreated(hoTarget))) Begin
Send CreateComObject of hoTarget
End
Get ComAppend Of hoTarget "qa_output/oneDrive/" To iSuccess
Get ComAppend Of hoTarget sLocalId To iSuccess
Get ComAppend Of hoTarget ".bin" To iSuccess
Get ComFileExists Of hoFac (ComGetAsString(hoTarget)) To bTemp1
If (bTemp1 = True) Begin
Showln "The output file already exists; nothing overwritten."
Procedure_Return
End
Get ComGetAsString Of hoTarget To sTemp1
Get ComWriteFile Of hoContents sTemp1 To iSuccess
If (iSuccess <> True) Begin
Showln "Could not write the downloaded version."
Procedure_Return
End
Get ComGetAsString Of hoTarget To sTemp1
Showln "Saved previous version: " sTemp1
End_Procedure