OneDrive: Synchronize a Local File Index with Delta Queries
See more OneDrive Examples
Build and maintain a local metadata index for one drive. The first run follows root/delta to a deltaLink; later runs start at that saved URL. Keep a separate checkpoint for each account, drive, and projection. Empty pages still have to be followed. OneDrive reports deletion with the deleted facet, not Outlook's @removed annotation.
Repeated item IDs are applied in received order, with the last occurrence winning. Partial updates preserve omitted fields; parent IDs support renames and moves without relying on returned paths. IDs remain strings and large sizes are stored as decimal text. Tombstones are retained rather than deleting local data. A real file synchronizer must download changed content, reconcile local edits, and remove a deleted folder only after the round is complete and it is empty.
The checkpoint stores data and continuation together, with a private matching backup before replacement. Use atomic durable storage and one writer in production. Intermediate pages are not a completed snapshot; roundComplete becomes true only at deltaLink. An expired state (410) requires reconciliation and a fresh baseline, not silent loss of local changes. The 100-page execution bound saves progress for the next run. Continuation URLs are private and must not be edited.
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.
Chilkat Visual FoxPro Downloads
LOCAL lnSuccess
LOCAL loToken
LOCAL loHttp
LOCAL loResp
LOCAL loResult
LOCAL lcDriveId
LOCAL lcAccountKey
LOCAL loFac
LOCAL lcCachePath
LOCAL lcBackupPath
LOCAL loState
LOCAL loEndpoint
LOCAL loHash
LOCAL loLink
LOCAL loItemPath
LOCAL loSeenLinks
LOCAL loPrevious
LOCAL lnPageCount
LOCAL lcUrl
LOCAL lnNextType
LOCAL lnDeltaType
LOCAL lnFinished
LOCAL lcResumeUrl
LOCAL i
LOCAL lcItemId
LOCAL lcItemKey
lnSuccess = 0
* Assumes Chilkat is unlocked. See Global Unlock Sample.
lnSuccess = 0
* Obtain a delegated Graph token with this example's file permissions.
* See OneDrive OAuth2 with PKCE.
loToken = CreateObject('Chilkat.JsonObject')
loToken.Strict = 1
lnSuccess = loToken.LoadFile("qa_data/tokens/oneDrive.json")
IF (lnSuccess <> 1) THEN
? "Could not load the private OneDrive token cache."
RELEASE loToken
CANCEL
ENDIF
IF ((loToken.JsonTypeOf("access_token") <> 1) OR (loToken.StringOfEquals("access_token","",1) = 1)) THEN
? "Expected a nonempty access_token in the JSON."
RELEASE loToken
CANCEL
ENDIF
loHttp = CreateObject('Chilkat.Http')
loHttp.AuthToken = loToken.StringOf("access_token")
loHttp.RequireSslCertVerify = 1
loHttp.FollowRedirects = 0
loHttp.ConnectTimeout = 20
loHttp.ReadTimeout = 120
loHttp.SetRequestHeader("Accept","application/json")
loResp = CreateObject('Chilkat.HttpResponse')
loResult = CreateObject('Chilkat.JsonObject')
loResult.Strict = 1
lcDriveId = "YOUR_DRIVE_ID"
* Use a distinct stable application account key and cache path per account.
lcAccountKey = "YOUR_LOCAL_ACCOUNT_KEY"
loFac = CreateObject('Chilkat.FileAccess')
lnSuccess = loFac.DirEnsureExists("qa_data/oneDrive")
IF (lnSuccess <> 1) THEN
? "Could not create the private synchronization directory."
RELEASE loToken
RELEASE loHttp
RELEASE loResp
RELEASE loResult
RELEASE loFac
CANCEL
ENDIF
lcCachePath = "qa_data/oneDrive/items-sync.json"
lcBackupPath = "qa_data/oneDrive/items-sync.json.bak"
loState = CreateObject('Chilkat.JsonObject')
loState.Strict = 1
IF (loFac.FileExists(lcCachePath) = 1) THEN
lnSuccess = loState.LoadFile(lcCachePath)
IF (lnSuccess <> 1) THEN
? "Invalid checkpoint; recover the matching data and cursor from the private .bak file before continuing."
RELEASE loToken
RELEASE loHttp
RELEASE loResp
RELEASE loResult
RELEASE loFac
RELEASE loState
CANCEL
ENDIF
IF ((loState.JsonTypeOf("resumeUrl") <> 1) OR (loState.StringOfEquals("resumeUrl","",1) = 1)) THEN
? "Expected a nonempty resumeUrl in the JSON."
RELEASE loToken
RELEASE loHttp
RELEASE loResp
RELEASE loResult
RELEASE loFac
RELEASE loState
CANCEL
ENDIF
IF ((loState.JsonTypeOf("driveId") <> 1) OR (loState.StringOfEquals("driveId","",1) = 1)) THEN
? "Expected a nonempty driveId in the JSON."
RELEASE loToken
RELEASE loHttp
RELEASE loResp
RELEASE loResult
RELEASE loFac
RELEASE loState
CANCEL
ENDIF
IF (loState.StringOfEquals("driveId",lcDriveId,1) <> 1) THEN
? "This checkpoint belongs to another drive. Use a separate cache."
RELEASE loToken
RELEASE loHttp
RELEASE loResp
RELEASE loResult
RELEASE loFac
RELEASE loState
CANCEL
ENDIF
IF (loState.StringOfEquals("accountKey",lcAccountKey,1) <> 1) THEN
? "This checkpoint belongs to another account; nothing requested."
RELEASE loToken
RELEASE loHttp
RELEASE loResp
RELEASE loResult
RELEASE loFac
RELEASE loState
CANCEL
ENDIF
IF (loState.JsonTypeOf("items") <> 3) THEN
? "Expected an items object in the checkpoint."
RELEASE loToken
RELEASE loHttp
RELEASE loResp
RELEASE loResult
RELEASE loFac
RELEASE loState
CANCEL
ENDIF
ELSE
loEndpoint = CreateObject('Chilkat.StringBuilder')
loEndpoint.Append("https://graph.microsoft.com/v1.0/drives/")
loEndpoint.Append(loHttp.UrlEncode(lcDriveId))
loEndpoint.Append("/root/delta?$select=id,name,size,parentReference,file,folder,eTag,deleted")
loState.UpdateString("driveId",lcDriveId)
loState.UpdateString("accountKey",lcAccountKey)
loState.UpdateString("resumeUrl",loEndpoint.GetAsString())
loState.UpdateNewObject("items")
ENDIF
loHash = CreateObject('Chilkat.Crypt2')
loHash.HashAlgorithm = "sha256"
loHash.Charset = "utf-8"
loHash.EncodingMode = "hex"
loLink = CreateObject('Chilkat.StringBuilder')
loItemPath = CreateObject('Chilkat.StringBuilder')
loSeenLinks = CreateObject('Chilkat.Hashtable')
loPrevious = CreateObject('Chilkat.JsonObject')
loPrevious.Strict = 1
lnPageCount = 0
DO WHILE (lnPageCount < 100)
lcUrl = loState.StringOf("resumeUrl")
loLink.Clear()
loLink.Append(lcUrl)
IF (loLink.StartsWith("https://graph.microsoft.com/v1.0/",1) <> 1) THEN
? "Unexpected continuation host or API version; no request sent."
RELEASE loToken
RELEASE loHttp
RELEASE loResp
RELEASE loResult
RELEASE loFac
RELEASE loState
RELEASE loEndpoint
RELEASE loHash
RELEASE loLink
RELEASE loItemPath
RELEASE loSeenLinks
RELEASE loPrevious
CANCEL
ENDIF
IF (loSeenLinks.Contains(lcUrl) = 1) THEN
? "Repeated continuation URL; stopping."
RELEASE loToken
RELEASE loHttp
RELEASE loResp
RELEASE loResult
RELEASE loFac
RELEASE loState
RELEASE loEndpoint
RELEASE loHash
RELEASE loLink
RELEASE loItemPath
RELEASE loSeenLinks
RELEASE loPrevious
CANCEL
ENDIF
loSeenLinks.AddInt(lcUrl,1)
lnSuccess = loHttp.HttpNoBody("GET",lcUrl,loResp)
* A false result can still include an HTTP error response.
IF ((lnSuccess <> 1) AND (loResp.StatusCode = 0)) THEN
? "No HTTP response was received. Inspect diagnostics privately; reconcile a write before retrying."
RELEASE loToken
RELEASE loHttp
RELEASE loResp
RELEASE loResult
RELEASE loFac
RELEASE loState
RELEASE loEndpoint
RELEASE loHash
RELEASE loLink
RELEASE loItemPath
RELEASE loSeenLinks
RELEASE loPrevious
CANCEL
ENDIF
IF (loResp.StatusCode = 410) THEN
? "Delta state expired. Reconcile local edits and rebuild a fresh drive baseline; preserve the existing cache."
RELEASE loToken
RELEASE loHttp
RELEASE loResp
RELEASE loResult
RELEASE loFac
RELEASE loState
RELEASE loEndpoint
RELEASE loHash
RELEASE loLink
RELEASE loItemPath
RELEASE loSeenLinks
RELEASE loPrevious
CANCEL
ENDIF
IF (loResp.StatusCode <> 200) THEN
? "Unexpected HTTP status: " + STR(loResp.StatusCode)
? "Request ID: " + loResp.GetHeaderField("request-id")
IF (loResp.StatusCode = 429) THEN
? "Retry-After: " + loResp.GetHeaderField("Retry-After")
ENDIF
IF (loResp.StatusCode = 401) THEN
? "Refresh the Graph access token or authorize again."
ENDIF
RELEASE loToken
RELEASE loHttp
RELEASE loResp
RELEASE loResult
RELEASE loFac
RELEASE loState
RELEASE loEndpoint
RELEASE loHash
RELEASE loLink
RELEASE loItemPath
RELEASE loSeenLinks
RELEASE loPrevious
CANCEL
ENDIF
lnSuccess = loResult.Load(loResp.BodyStr)
IF (lnSuccess <> 1) THEN
? "Expected a well-formed JSON response."
RELEASE loToken
RELEASE loHttp
RELEASE loResp
RELEASE loResult
RELEASE loFac
RELEASE loState
RELEASE loEndpoint
RELEASE loHash
RELEASE loLink
RELEASE loItemPath
RELEASE loSeenLinks
RELEASE loPrevious
CANCEL
ENDIF
IF (loResult.JsonTypeOf("value") <> 4) THEN
? "Expected a value array; checkpoint unchanged."
RELEASE loToken
RELEASE loHttp
RELEASE loResp
RELEASE loResult
RELEASE loFac
RELEASE loState
RELEASE loEndpoint
RELEASE loHash
RELEASE loLink
RELEASE loItemPath
RELEASE loSeenLinks
RELEASE loPrevious
CANCEL
ENDIF
* Exactly one continuation member must be a nonempty string.
lnNextType = loResult.JsonTypeOf('"@odata.nextLink"')
lnDeltaType = loResult.JsonTypeOf('"@odata.deltaLink"')
lnFinished = 0
IF ((lnNextType = 1) AND (lnDeltaType = -1)) THEN
lcResumeUrl = loResult.StringOf('"@odata.nextLink"')
ELSE
IF ((lnDeltaType <> 1) OR (lnNextType <> -1)) THEN
? "Invalid delta continuation; checkpoint unchanged."
RELEASE loToken
RELEASE loHttp
RELEASE loResp
RELEASE loResult
RELEASE loFac
RELEASE loState
RELEASE loEndpoint
RELEASE loHash
RELEASE loLink
RELEASE loItemPath
RELEASE loSeenLinks
RELEASE loPrevious
CANCEL
ENDIF
lcResumeUrl = loResult.StringOf('"@odata.deltaLink"')
lnFinished = 1
ENDIF
loLink.Clear()
loLink.Append(lcResumeUrl)
IF (loLink.StartsWith("https://graph.microsoft.com/v1.0/",1) <> 1) THEN
? "Invalid continuation URL; checkpoint unchanged."
RELEASE loToken
RELEASE loHttp
RELEASE loResp
RELEASE loResult
RELEASE loFac
RELEASE loState
RELEASE loEndpoint
RELEASE loHash
RELEASE loLink
RELEASE loItemPath
RELEASE loSeenLinks
RELEASE loPrevious
CANCEL
ENDIF
lnSuccess = loPrevious.Load(loState.Emit())
IF (lnSuccess <> 1) THEN
? "Could not snapshot the previous checkpoint."
RELEASE loToken
RELEASE loHttp
RELEASE loResp
RELEASE loResult
RELEASE loFac
RELEASE loState
RELEASE loEndpoint
RELEASE loHash
RELEASE loLink
RELEASE loItemPath
RELEASE loSeenLinks
RELEASE loPrevious
CANCEL
ENDIF
i = 0
DO WHILE (i < loResult.SizeOfArray("value"))
loResult.I = i
IF ((loResult.JsonTypeOf("value[i].id") <> 1) OR (loResult.StringOfEquals("value[i].id","",1) = 1)) THEN
? "Expected a nonempty value[i].id in the JSON."
RELEASE loToken
RELEASE loHttp
RELEASE loResp
RELEASE loResult
RELEASE loFac
RELEASE loState
RELEASE loEndpoint
RELEASE loHash
RELEASE loLink
RELEASE loItemPath
RELEASE loSeenLinks
RELEASE loPrevious
CANCEL
ENDIF
lcItemId = loResult.StringOf("value[i].id")
lcItemKey = loHash.HashStringENC(lcItemId)
IF (loHash.LastMethodSuccess <> 1) THEN
? "Could not form a local record key."
RELEASE loToken
RELEASE loHttp
RELEASE loResp
RELEASE loResult
RELEASE loFac
RELEASE loState
RELEASE loEndpoint
RELEASE loHash
RELEASE loLink
RELEASE loItemPath
RELEASE loSeenLinks
RELEASE loPrevious
CANCEL
ENDIF
* Hash only for a JSON-safe local key. Preserve the original opaque ID in the record.
loItemPath.Clear()
loItemPath.Append("items.")
loItemPath.Append(lcItemKey)
IF (loState.JsonTypeOf(loItemPath.GetAsString()) <> 3) THEN
loState.UpdateNewObject(loItemPath.GetAsString())
ENDIF
loItemPath.Append(".")
loState.PathPrefix = loItemPath.GetAsString()
loState.UpdateString("id",lcItemId)
IF (loResult.JsonTypeOf("value[i].deleted") = 3) THEN
* Retain a tombstone; this example does not delete local files or folders.
* Process folder deletion only after the full round and only when the folder is empty.
loState.UpdateBool("removed",1)
? "Removed from the local view: " + lcItemId
ELSE
loState.UpdateBool("removed",0)
IF (loResult.HasMember("value[i].name") = 1) THEN
IF ((loResult.JsonTypeOf("value[i].name") <> 1) OR (loResult.StringOfEquals("value[i].name","",1) = 1)) THEN
? "Expected a nonempty value[i].name in the JSON."
RELEASE loToken
RELEASE loHttp
RELEASE loResp
RELEASE loResult
RELEASE loFac
RELEASE loState
RELEASE loEndpoint
RELEASE loHash
RELEASE loLink
RELEASE loItemPath
RELEASE loSeenLinks
RELEASE loPrevious
CANCEL
ENDIF
loState.UpdateString("name",loResult.StringOf("value[i].name"))
ENDIF
IF (loResult.HasMember("value[i].eTag") = 1) THEN
IF ((loResult.JsonTypeOf("value[i].eTag") <> 1) OR (loResult.StringOfEquals("value[i].eTag","",1) = 1)) THEN
? "Expected a nonempty value[i].eTag in the JSON."
RELEASE loToken
RELEASE loHttp
RELEASE loResp
RELEASE loResult
RELEASE loFac
RELEASE loState
RELEASE loEndpoint
RELEASE loHash
RELEASE loLink
RELEASE loItemPath
RELEASE loSeenLinks
RELEASE loPrevious
CANCEL
ENDIF
loState.UpdateString("eTag",loResult.StringOf("value[i].eTag"))
ENDIF
IF (loResult.HasMember("value[i].parentReference.id") = 1) THEN
IF ((loResult.JsonTypeOf("value[i].parentReference.id") <> 1) OR (loResult.StringOfEquals("value[i].parentReference.id","",1) = 1)) THEN
? "Expected a nonempty value[i].parentReference.id in the JSON."
RELEASE loToken
RELEASE loHttp
RELEASE loResp
RELEASE loResult
RELEASE loFac
RELEASE loState
RELEASE loEndpoint
RELEASE loHash
RELEASE loLink
RELEASE loItemPath
RELEASE loSeenLinks
RELEASE loPrevious
CANCEL
ENDIF
loState.UpdateString("parentReference.id",loResult.StringOf("value[i].parentReference.id"))
ENDIF
IF (loResult.HasMember("value[i].parentReference.driveId") = 1) THEN
IF ((loResult.JsonTypeOf("value[i].parentReference.driveId") <> 1) OR (loResult.StringOfEquals("value[i].parentReference.driveId","",1) = 1)) THEN
? "Expected a nonempty value[i].parentReference.driveId in the JSON."
RELEASE loToken
RELEASE loHttp
RELEASE loResp
RELEASE loResult
RELEASE loFac
RELEASE loState
RELEASE loEndpoint
RELEASE loHash
RELEASE loLink
RELEASE loItemPath
RELEASE loSeenLinks
RELEASE loPrevious
CANCEL
ENDIF
loState.UpdateString("parentReference.driveId",loResult.StringOf("value[i].parentReference.driveId"))
ENDIF
* Omitted fields are unchanged. Do not erase an earlier name or parent on a partial delta.
* Keep large sizes as decimal text in this demonstration's local index.
IF (loResult.HasMember("value[i].size") = 1) THEN
loState.UpdateString("sizeBytes",loResult.StringOf("value[i].size"))
ENDIF
IF (loResult.JsonTypeOf("value[i].folder") = 3) THEN
loState.UpdateString("kind","folder")
ENDIF
IF (loResult.JsonTypeOf("value[i].file") = 3) THEN
loState.UpdateString("kind","file")
ENDIF
loState.PathPrefix = ""
? "Updated local item: " + lcItemId
ENDIF
loState.PathPrefix = ""
i = i + 1
ENDDO
loState.UpdateString("resumeUrl",lcResumeUrl)
loState.UpdateBool("roundComplete",lnFinished)
* Save the data and cursor in one document. The backup contains the matching previous pair.
lnSuccess = loPrevious.WriteFile(lcBackupPath)
IF (lnSuccess <> 1) THEN
? "Could not back up the previous checkpoint; no replacement attempted."
RELEASE loToken
RELEASE loHttp
RELEASE loResp
RELEASE loResult
RELEASE loFac
RELEASE loState
RELEASE loEndpoint
RELEASE loHash
RELEASE loLink
RELEASE loItemPath
RELEASE loSeenLinks
RELEASE loPrevious
CANCEL
ENDIF
lnSuccess = loState.WriteFile(lcCachePath)
IF (lnSuccess <> 1) THEN
? "Checkpoint write failed. Restore the private .bak before continuing."
RELEASE loToken
RELEASE loHttp
RELEASE loResp
RELEASE loResult
RELEASE loFac
RELEASE loState
RELEASE loEndpoint
RELEASE loHash
RELEASE loLink
RELEASE loItemPath
RELEASE loSeenLinks
RELEASE loPrevious
CANCEL
ENDIF
lnPageCount = lnPageCount + 1
IF (lnFinished = 1) THEN
? "Synchronization round complete; saved the delta link for the next run."
RELEASE loToken
RELEASE loHttp
RELEASE loResp
RELEASE loResult
RELEASE loFac
RELEASE loState
RELEASE loEndpoint
RELEASE loHash
RELEASE loLink
RELEASE loItemPath
RELEASE loSeenLinks
RELEASE loPrevious
CANCEL
ENDIF
ENDDO
? "Paused after 100 pages; the saved nextLink resumes on the next run."
RELEASE loToken
RELEASE loHttp
RELEASE loResp
RELEASE loResult
RELEASE loFac
RELEASE loState
RELEASE loEndpoint
RELEASE loHash
RELEASE loLink
RELEASE loItemPath
RELEASE loSeenLinks
RELEASE loPrevious