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 SQL Server Downloads
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
DECLARE @iTmp0 int
DECLARE @iTmp1 int
DECLARE @sTmp0 nvarchar(4000)
DECLARE @success int
SELECT @success = 0
-- Assumes Chilkat is unlocked. See Global Unlock Sample.
SELECT @success = 0
-- Obtain a delegated Graph token with this example's file permissions.
-- See OneDrive OAuth2 with PKCE.
DECLARE @token int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @token OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
EXEC sp_OASetProperty @token, 'Strict', 1
EXEC sp_OAMethod @token, 'LoadFile', @success OUT, 'qa_data/tokens/oneDrive.json'
IF @success <> 1
BEGIN
PRINT 'Could not load the private OneDrive token cache.'
EXEC @hr = sp_OADestroy @token
RETURN
END
EXEC sp_OAMethod @token, 'JsonTypeOf', @iTmp0 OUT, 'access_token'
EXEC sp_OAMethod @token, 'StringOfEquals', @iTmp1 OUT, 'access_token', '', 1
IF (@iTmp0 <> 1) or (@iTmp1 = 1)
BEGIN
PRINT 'Expected a nonempty access_token in the JSON.'
EXEC @hr = sp_OADestroy @token
RETURN
END
DECLARE @http int
EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
EXEC sp_OAMethod @token, 'StringOf', @sTmp0 OUT, 'access_token'
EXEC sp_OASetProperty @http, 'AuthToken', @sTmp0
EXEC sp_OASetProperty @http, 'RequireSslCertVerify', 1
EXEC sp_OASetProperty @http, 'FollowRedirects', 0
EXEC sp_OASetProperty @http, 'ConnectTimeout', 20
EXEC sp_OASetProperty @http, 'ReadTimeout', 120
EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Accept', 'application/json'
DECLARE @resp int
EXEC @hr = sp_OACreate 'Chilkat.HttpResponse', @resp OUT
DECLARE @result int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @result OUT
EXEC sp_OASetProperty @result, 'Strict', 1
DECLARE @driveId nvarchar(4000)
SELECT @driveId = 'YOUR_DRIVE_ID'
-- Use a distinct stable application account key and cache path per account.
DECLARE @accountKey nvarchar(4000)
SELECT @accountKey = 'YOUR_LOCAL_ACCOUNT_KEY'
DECLARE @fac int
EXEC @hr = sp_OACreate 'Chilkat.FileAccess', @fac OUT
EXEC sp_OAMethod @fac, 'DirEnsureExists', @success OUT, 'qa_data/oneDrive'
IF @success <> 1
BEGIN
PRINT 'Could not create the private synchronization directory.'
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @fac
RETURN
END
DECLARE @cachePath nvarchar(4000)
SELECT @cachePath = 'qa_data/oneDrive/items-sync.json'
DECLARE @backupPath nvarchar(4000)
SELECT @backupPath = 'qa_data/oneDrive/items-sync.json.bak'
DECLARE @state int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @state OUT
EXEC sp_OASetProperty @state, 'Strict', 1
EXEC sp_OAMethod @fac, 'FileExists', @iTmp0 OUT, @cachePath
IF @iTmp0 = 1
BEGIN
EXEC sp_OAMethod @state, 'LoadFile', @success OUT, @cachePath
IF @success <> 1
BEGIN
PRINT 'Invalid checkpoint; recover the matching data and cursor from the private .bak file before continuing.'
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @state
RETURN
END
EXEC sp_OAMethod @state, 'JsonTypeOf', @iTmp0 OUT, 'resumeUrl'
EXEC sp_OAMethod @state, 'StringOfEquals', @iTmp1 OUT, 'resumeUrl', '', 1
IF (@iTmp0 <> 1) or (@iTmp1 = 1)
BEGIN
PRINT 'Expected a nonempty resumeUrl in the JSON.'
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @state
RETURN
END
EXEC sp_OAMethod @state, 'JsonTypeOf', @iTmp0 OUT, 'driveId'
EXEC sp_OAMethod @state, 'StringOfEquals', @iTmp1 OUT, 'driveId', '', 1
IF (@iTmp0 <> 1) or (@iTmp1 = 1)
BEGIN
PRINT 'Expected a nonempty driveId in the JSON.'
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @state
RETURN
END
EXEC sp_OAMethod @state, 'StringOfEquals', @iTmp0 OUT, 'driveId', @driveId, 1
IF @iTmp0 <> 1
BEGIN
PRINT 'This checkpoint belongs to another drive. Use a separate cache.'
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @state
RETURN
END
EXEC sp_OAMethod @state, 'StringOfEquals', @iTmp0 OUT, 'accountKey', @accountKey, 1
IF @iTmp0 <> 1
BEGIN
PRINT 'This checkpoint belongs to another account; nothing requested.'
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @state
RETURN
END
EXEC sp_OAMethod @state, 'JsonTypeOf', @iTmp0 OUT, 'items'
IF @iTmp0 <> 3
BEGIN
PRINT 'Expected an items object in the checkpoint.'
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @state
RETURN
END
END
ELSE
BEGIN
DECLARE @endpoint int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @endpoint OUT
EXEC sp_OAMethod @endpoint, 'Append', @success OUT, 'https://graph.microsoft.com/v1.0/drives/'
EXEC sp_OAMethod @http, 'UrlEncode', @sTmp0 OUT, @driveId
EXEC sp_OAMethod @endpoint, 'Append', @success OUT, @sTmp0
EXEC sp_OAMethod @endpoint, 'Append', @success OUT, '/root/delta?$select=id,name,size,parentReference,file,folder,eTag,deleted'
EXEC sp_OAMethod @state, 'UpdateString', @success OUT, 'driveId', @driveId
EXEC sp_OAMethod @state, 'UpdateString', @success OUT, 'accountKey', @accountKey
EXEC sp_OAMethod @endpoint, 'GetAsString', @sTmp0 OUT
EXEC sp_OAMethod @state, 'UpdateString', @success OUT, 'resumeUrl', @sTmp0
EXEC sp_OAMethod @state, 'UpdateNewObject', @success OUT, 'items'
END
DECLARE @hash int
EXEC @hr = sp_OACreate 'Chilkat.Crypt2', @hash OUT
EXEC sp_OASetProperty @hash, 'HashAlgorithm', 'sha256'
EXEC sp_OASetProperty @hash, 'Charset', 'utf-8'
EXEC sp_OASetProperty @hash, 'EncodingMode', 'hex'
DECLARE @link int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @link OUT
DECLARE @itemPath int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @itemPath OUT
DECLARE @seenLinks int
EXEC @hr = sp_OACreate 'Chilkat.Hashtable', @seenLinks OUT
DECLARE @previous int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @previous OUT
EXEC sp_OASetProperty @previous, 'Strict', 1
DECLARE @pageCount int
SELECT @pageCount = 0
WHILE (@pageCount < 100)
BEGIN
DECLARE @url nvarchar(4000)
EXEC sp_OAMethod @state, 'StringOf', @url OUT, 'resumeUrl'
EXEC sp_OAMethod @link, 'Clear', NULL
EXEC sp_OAMethod @link, 'Append', @success OUT, @url
EXEC sp_OAMethod @link, 'StartsWith', @iTmp0 OUT, 'https://graph.microsoft.com/v1.0/', 1
IF @iTmp0 <> 1
BEGIN
PRINT 'Unexpected continuation host or API version; no request sent.'
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @state
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @hash
EXEC @hr = sp_OADestroy @link
EXEC @hr = sp_OADestroy @itemPath
EXEC @hr = sp_OADestroy @seenLinks
EXEC @hr = sp_OADestroy @previous
RETURN
END
EXEC sp_OAMethod @seenLinks, 'Contains', @iTmp0 OUT, @url
IF @iTmp0 = 1
BEGIN
PRINT 'Repeated continuation URL; stopping.'
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @state
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @hash
EXEC @hr = sp_OADestroy @link
EXEC @hr = sp_OADestroy @itemPath
EXEC @hr = sp_OADestroy @seenLinks
EXEC @hr = sp_OADestroy @previous
RETURN
END
EXEC sp_OAMethod @seenLinks, 'AddInt', @success OUT, @url, 1
EXEC sp_OAMethod @http, 'HttpNoBody', @success OUT, 'GET', @url, @resp
-- A false result can still include an HTTP error response.
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
IF (@success <> 1) and (@iTmp0 = 0)
BEGIN
PRINT 'No HTTP response was received. Inspect diagnostics privately; reconcile a write before retrying.'
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @state
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @hash
EXEC @hr = sp_OADestroy @link
EXEC @hr = sp_OADestroy @itemPath
EXEC @hr = sp_OADestroy @seenLinks
EXEC @hr = sp_OADestroy @previous
RETURN
END
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
IF @iTmp0 = 410
BEGIN
PRINT 'Delta state expired. Reconcile local edits and rebuild a fresh drive baseline; preserve the existing cache.'
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @state
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @hash
EXEC @hr = sp_OADestroy @link
EXEC @hr = sp_OADestroy @itemPath
EXEC @hr = sp_OADestroy @seenLinks
EXEC @hr = sp_OADestroy @previous
RETURN
END
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
IF @iTmp0 <> 200
BEGIN
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
PRINT 'Unexpected HTTP status: ' + @iTmp0
EXEC sp_OAMethod @resp, 'GetHeaderField', @sTmp0 OUT, 'request-id'
PRINT 'Request ID: ' + @sTmp0
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
IF @iTmp0 = 429
BEGIN
EXEC sp_OAMethod @resp, 'GetHeaderField', @sTmp0 OUT, 'Retry-After'
PRINT 'Retry-After: ' + @sTmp0
END
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
IF @iTmp0 = 401
BEGIN
PRINT 'Refresh the Graph access token or authorize again.'
END
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @state
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @hash
EXEC @hr = sp_OADestroy @link
EXEC @hr = sp_OADestroy @itemPath
EXEC @hr = sp_OADestroy @seenLinks
EXEC @hr = sp_OADestroy @previous
RETURN
END
EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
EXEC sp_OAMethod @result, 'Load', @success OUT, @sTmp0
IF @success <> 1
BEGIN
PRINT 'Expected a well-formed JSON response.'
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @state
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @hash
EXEC @hr = sp_OADestroy @link
EXEC @hr = sp_OADestroy @itemPath
EXEC @hr = sp_OADestroy @seenLinks
EXEC @hr = sp_OADestroy @previous
RETURN
END
EXEC sp_OAMethod @result, 'JsonTypeOf', @iTmp0 OUT, 'value'
IF @iTmp0 <> 4
BEGIN
PRINT 'Expected a value array; checkpoint unchanged.'
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @state
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @hash
EXEC @hr = sp_OADestroy @link
EXEC @hr = sp_OADestroy @itemPath
EXEC @hr = sp_OADestroy @seenLinks
EXEC @hr = sp_OADestroy @previous
RETURN
END
-- Exactly one continuation member must be a nonempty string.
DECLARE @nextType int
EXEC sp_OAMethod @result, 'JsonTypeOf', @nextType OUT, '"@odata.nextLink"'
DECLARE @deltaType int
EXEC sp_OAMethod @result, 'JsonTypeOf', @deltaType OUT, '"@odata.deltaLink"'
DECLARE @finished int
SELECT @finished = 0
DECLARE @resumeUrl nvarchar(4000)
IF (@nextType = 1) and (@deltaType = -1)
BEGIN
EXEC sp_OAMethod @result, 'StringOf', @resumeUrl OUT, '"@odata.nextLink"'
END
ELSE
BEGIN
IF (@deltaType <> 1) or (@nextType <> -1)
BEGIN
PRINT 'Invalid delta continuation; checkpoint unchanged.'
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @state
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @hash
EXEC @hr = sp_OADestroy @link
EXEC @hr = sp_OADestroy @itemPath
EXEC @hr = sp_OADestroy @seenLinks
EXEC @hr = sp_OADestroy @previous
RETURN
END
EXEC sp_OAMethod @result, 'StringOf', @resumeUrl OUT, '"@odata.deltaLink"'
SELECT @finished = 1
END
EXEC sp_OAMethod @link, 'Clear', NULL
EXEC sp_OAMethod @link, 'Append', @success OUT, @resumeUrl
EXEC sp_OAMethod @link, 'StartsWith', @iTmp0 OUT, 'https://graph.microsoft.com/v1.0/', 1
IF @iTmp0 <> 1
BEGIN
PRINT 'Invalid continuation URL; checkpoint unchanged.'
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @state
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @hash
EXEC @hr = sp_OADestroy @link
EXEC @hr = sp_OADestroy @itemPath
EXEC @hr = sp_OADestroy @seenLinks
EXEC @hr = sp_OADestroy @previous
RETURN
END
EXEC sp_OAMethod @state, 'Emit', @sTmp0 OUT
EXEC sp_OAMethod @previous, 'Load', @success OUT, @sTmp0
IF @success <> 1
BEGIN
PRINT 'Could not snapshot the previous checkpoint.'
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @state
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @hash
EXEC @hr = sp_OADestroy @link
EXEC @hr = sp_OADestroy @itemPath
EXEC @hr = sp_OADestroy @seenLinks
EXEC @hr = sp_OADestroy @previous
RETURN
END
DECLARE @i int
SELECT @i = 0
EXEC sp_OAMethod @result, 'SizeOfArray', @iTmp0 OUT, 'value'
WHILE (@i < @iTmp0)
BEGIN
EXEC sp_OASetProperty @result, 'I', @i
EXEC sp_OAMethod @result, 'JsonTypeOf', @iTmp0 OUT, 'value[i].id'
EXEC sp_OAMethod @result, 'StringOfEquals', @iTmp1 OUT, 'value[i].id', '', 1
IF (@iTmp0 <> 1) or (@iTmp1 = 1)
BEGIN
PRINT 'Expected a nonempty value[i].id in the JSON.'
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @state
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @hash
EXEC @hr = sp_OADestroy @link
EXEC @hr = sp_OADestroy @itemPath
EXEC @hr = sp_OADestroy @seenLinks
EXEC @hr = sp_OADestroy @previous
RETURN
END
DECLARE @itemId nvarchar(4000)
EXEC sp_OAMethod @result, 'StringOf', @itemId OUT, 'value[i].id'
DECLARE @itemKey nvarchar(4000)
EXEC sp_OAMethod @hash, 'HashStringENC', @itemKey OUT, @itemId
EXEC sp_OAGetProperty @hash, 'LastMethodSuccess', @iTmp0 OUT
IF @iTmp0 <> 1
BEGIN
PRINT 'Could not form a local record key.'
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @state
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @hash
EXEC @hr = sp_OADestroy @link
EXEC @hr = sp_OADestroy @itemPath
EXEC @hr = sp_OADestroy @seenLinks
EXEC @hr = sp_OADestroy @previous
RETURN
END
-- Hash only for a JSON-safe local key. Preserve the original opaque ID in the record.
EXEC sp_OAMethod @itemPath, 'Clear', NULL
EXEC sp_OAMethod @itemPath, 'Append', @success OUT, 'items.'
EXEC sp_OAMethod @itemPath, 'Append', @success OUT, @itemKey
EXEC sp_OAMethod @itemPath, 'GetAsString', @sTmp0 OUT
EXEC sp_OAMethod @state, 'JsonTypeOf', @iTmp0 OUT, @sTmp0
IF @iTmp0 <> 3
BEGIN
EXEC sp_OAMethod @itemPath, 'GetAsString', @sTmp0 OUT
EXEC sp_OAMethod @state, 'UpdateNewObject', @success OUT, @sTmp0
END
EXEC sp_OAMethod @itemPath, 'Append', @success OUT, '.'
EXEC sp_OAMethod @itemPath, 'GetAsString', @sTmp0 OUT
EXEC sp_OASetProperty @state, 'PathPrefix', @sTmp0
EXEC sp_OAMethod @state, 'UpdateString', @success OUT, 'id', @itemId
EXEC sp_OAMethod @result, 'JsonTypeOf', @iTmp0 OUT, 'value[i].deleted'
IF @iTmp0 = 3
BEGIN
-- 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.
EXEC sp_OAMethod @state, 'UpdateBool', @success OUT, 'removed', 1
PRINT 'Removed from the local view: ' + @itemId
END
ELSE
BEGIN
EXEC sp_OAMethod @state, 'UpdateBool', @success OUT, 'removed', 0
EXEC sp_OAMethod @result, 'HasMember', @iTmp0 OUT, 'value[i].name'
IF @iTmp0 = 1
BEGIN
EXEC sp_OAMethod @result, 'JsonTypeOf', @iTmp0 OUT, 'value[i].name'
EXEC sp_OAMethod @result, 'StringOfEquals', @iTmp1 OUT, 'value[i].name', '', 1
IF (@iTmp0 <> 1) or (@iTmp1 = 1)
BEGIN
PRINT 'Expected a nonempty value[i].name in the JSON.'
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @state
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @hash
EXEC @hr = sp_OADestroy @link
EXEC @hr = sp_OADestroy @itemPath
EXEC @hr = sp_OADestroy @seenLinks
EXEC @hr = sp_OADestroy @previous
RETURN
END
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'value[i].name'
EXEC sp_OAMethod @state, 'UpdateString', @success OUT, 'name', @sTmp0
END
EXEC sp_OAMethod @result, 'HasMember', @iTmp0 OUT, 'value[i].eTag'
IF @iTmp0 = 1
BEGIN
EXEC sp_OAMethod @result, 'JsonTypeOf', @iTmp0 OUT, 'value[i].eTag'
EXEC sp_OAMethod @result, 'StringOfEquals', @iTmp1 OUT, 'value[i].eTag', '', 1
IF (@iTmp0 <> 1) or (@iTmp1 = 1)
BEGIN
PRINT 'Expected a nonempty value[i].eTag in the JSON.'
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @state
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @hash
EXEC @hr = sp_OADestroy @link
EXEC @hr = sp_OADestroy @itemPath
EXEC @hr = sp_OADestroy @seenLinks
EXEC @hr = sp_OADestroy @previous
RETURN
END
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'value[i].eTag'
EXEC sp_OAMethod @state, 'UpdateString', @success OUT, 'eTag', @sTmp0
END
EXEC sp_OAMethod @result, 'HasMember', @iTmp0 OUT, 'value[i].parentReference.id'
IF @iTmp0 = 1
BEGIN
EXEC sp_OAMethod @result, 'JsonTypeOf', @iTmp0 OUT, 'value[i].parentReference.id'
EXEC sp_OAMethod @result, 'StringOfEquals', @iTmp1 OUT, 'value[i].parentReference.id', '', 1
IF (@iTmp0 <> 1) or (@iTmp1 = 1)
BEGIN
PRINT 'Expected a nonempty value[i].parentReference.id in the JSON.'
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @state
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @hash
EXEC @hr = sp_OADestroy @link
EXEC @hr = sp_OADestroy @itemPath
EXEC @hr = sp_OADestroy @seenLinks
EXEC @hr = sp_OADestroy @previous
RETURN
END
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'value[i].parentReference.id'
EXEC sp_OAMethod @state, 'UpdateString', @success OUT, 'parentReference.id', @sTmp0
END
EXEC sp_OAMethod @result, 'HasMember', @iTmp0 OUT, 'value[i].parentReference.driveId'
IF @iTmp0 = 1
BEGIN
EXEC sp_OAMethod @result, 'JsonTypeOf', @iTmp0 OUT, 'value[i].parentReference.driveId'
EXEC sp_OAMethod @result, 'StringOfEquals', @iTmp1 OUT, 'value[i].parentReference.driveId', '', 1
IF (@iTmp0 <> 1) or (@iTmp1 = 1)
BEGIN
PRINT 'Expected a nonempty value[i].parentReference.driveId in the JSON.'
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @state
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @hash
EXEC @hr = sp_OADestroy @link
EXEC @hr = sp_OADestroy @itemPath
EXEC @hr = sp_OADestroy @seenLinks
EXEC @hr = sp_OADestroy @previous
RETURN
END
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'value[i].parentReference.driveId'
EXEC sp_OAMethod @state, 'UpdateString', @success OUT, 'parentReference.driveId', @sTmp0
END
-- 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.
EXEC sp_OAMethod @result, 'HasMember', @iTmp0 OUT, 'value[i].size'
IF @iTmp0 = 1
BEGIN
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'value[i].size'
EXEC sp_OAMethod @state, 'UpdateString', @success OUT, 'sizeBytes', @sTmp0
END
EXEC sp_OAMethod @result, 'JsonTypeOf', @iTmp0 OUT, 'value[i].folder'
IF @iTmp0 = 3
BEGIN
EXEC sp_OAMethod @state, 'UpdateString', @success OUT, 'kind', 'folder'
END
EXEC sp_OAMethod @result, 'JsonTypeOf', @iTmp0 OUT, 'value[i].file'
IF @iTmp0 = 3
BEGIN
EXEC sp_OAMethod @state, 'UpdateString', @success OUT, 'kind', 'file'
END
EXEC sp_OASetProperty @state, 'PathPrefix', ''
PRINT 'Updated local item: ' + @itemId
END
EXEC sp_OASetProperty @state, 'PathPrefix', ''
SELECT @i = @i + 1
END
EXEC sp_OAMethod @state, 'UpdateString', @success OUT, 'resumeUrl', @resumeUrl
EXEC sp_OAMethod @state, 'UpdateBool', @success OUT, 'roundComplete', @finished
-- Save the data and cursor in one document. The backup contains the matching previous pair.
EXEC sp_OAMethod @previous, 'WriteFile', @success OUT, @backupPath
IF @success <> 1
BEGIN
PRINT 'Could not back up the previous checkpoint; no replacement attempted.'
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @state
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @hash
EXEC @hr = sp_OADestroy @link
EXEC @hr = sp_OADestroy @itemPath
EXEC @hr = sp_OADestroy @seenLinks
EXEC @hr = sp_OADestroy @previous
RETURN
END
EXEC sp_OAMethod @state, 'WriteFile', @success OUT, @cachePath
IF @success <> 1
BEGIN
PRINT 'Checkpoint write failed. Restore the private .bak before continuing.'
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @state
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @hash
EXEC @hr = sp_OADestroy @link
EXEC @hr = sp_OADestroy @itemPath
EXEC @hr = sp_OADestroy @seenLinks
EXEC @hr = sp_OADestroy @previous
RETURN
END
SELECT @pageCount = @pageCount + 1
IF @finished = 1
BEGIN
PRINT 'Synchronization round complete; saved the delta link for the next run.'
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @state
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @hash
EXEC @hr = sp_OADestroy @link
EXEC @hr = sp_OADestroy @itemPath
EXEC @hr = sp_OADestroy @seenLinks
EXEC @hr = sp_OADestroy @previous
RETURN
END
END
PRINT 'Paused after 100 pages; the saved nextLink resumes on the next run.'
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @state
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @hash
EXEC @hr = sp_OADestroy @link
EXEC @hr = sp_OADestroy @itemPath
EXEC @hr = sp_OADestroy @seenLinks
EXEC @hr = sp_OADestroy @previous
END
GO