OneDrive: Copy a File or Folder and Monitor Completion
See more OneDrive Examples
Creates a new item: copy a selected item to an explicitly addressed destination folder. A folder copy includes its descendants. The first run submits the request and saves its monitor URL; subsequent runs make one status request without submitting another copy. Use a fresh private receipt for each intentional operation and account. Schedule checks with backoff and honor Retry-After; 202 means accepted, not finished.
The receipt starts as pending before submission. A lost response or failed write requires reconciling the destination before starting again. Completed and failed receipts prevent automatic resubmission. Monitoring accepts JSON completion and 303 completion responses without automatically following redirects. A resourceId, when returned, is the new item ID and stays a string. Monitor URLs can expire; they are private, temporary operation state.
The default conflict behavior fails on a name collision. Copies inherit destination permissions and do not retain all source metadata. This example copies current content only. Copying all versions requires includeAllVersionHistory=true; Microsoft documents a current issue when it is combined with name, so copy first without name and rename afterward for that workflow. Folder copies are limited to 30,000 items; app-only cross-geo copy is unsupported.
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.ReadWrite.
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 @iTmp2 int
DECLARE @sTmp0 nvarchar(4000)
DECLARE @sTmp1 nvarchar(4000)
DECLARE @sTmp2 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'
DECLARE @itemId nvarchar(4000)
SELECT @itemId = 'YOUR_ITEM_ID'
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, '/items/'
EXEC sp_OAMethod @http, 'UrlEncode', @sTmp0 OUT, @itemId
EXEC sp_OAMethod @endpoint, 'Append', @success OUT, @sTmp0
DECLARE @destinationDriveId nvarchar(4000)
SELECT @destinationDriveId = 'YOUR_DESTINATION_DRIVE_ID'
DECLARE @destinationFolderId nvarchar(4000)
SELECT @destinationFolderId = 'YOUR_DESTINATION_FOLDER_ID'
DECLARE @newName nvarchar(4000)
SELECT @newName = 'copied-file.txt'
-- Use a separate private receipt for each copy operation and account.
DECLARE @accountKey nvarchar(4000)
SELECT @accountKey = 'YOUR_LOCAL_ACCOUNT_KEY'
DECLARE @receiptPath nvarchar(4000)
SELECT @receiptPath = 'qa_data/oneDrive/copy-receipt.json'
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 receipt 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 @endpoint
EXEC @hr = sp_OADestroy @fac
RETURN
END
DECLARE @receipt int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @receipt OUT
EXEC sp_OASetProperty @receipt, 'Strict', 1
EXEC sp_OAMethod @fac, 'FileExists', @iTmp0 OUT, @receiptPath
IF @iTmp0 = 1
BEGIN
EXEC sp_OAMethod @receipt, 'LoadFile', @success OUT, @receiptPath
IF @success <> 1
BEGIN
PRINT 'Invalid receipt; reconcile the existing operation before starting another.'
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @receipt
RETURN
END
EXEC sp_OAMethod @receipt, 'StringOfEquals', @iTmp0 OUT, 'accountKey', @accountKey, 1
IF @iTmp0 <> 1
BEGIN
PRINT 'Receipt belongs to another copy operation; 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 @endpoint
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @receipt
RETURN
END
EXEC sp_OAMethod @receipt, 'StringOfEquals', @iTmp0 OUT, 'sourceDriveId', @driveId, 1
IF @iTmp0 <> 1
BEGIN
PRINT 'Receipt belongs to another copy operation; 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 @endpoint
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @receipt
RETURN
END
EXEC sp_OAMethod @receipt, 'StringOfEquals', @iTmp0 OUT, 'sourceItemId', @itemId, 1
IF @iTmp0 <> 1
BEGIN
PRINT 'Receipt belongs to another copy operation; 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 @endpoint
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @receipt
RETURN
END
EXEC sp_OAMethod @receipt, 'StringOfEquals', @iTmp0 OUT, 'destinationDriveId', @destinationDriveId, 1
IF @iTmp0 <> 1
BEGIN
PRINT 'Receipt belongs to another copy operation; 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 @endpoint
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @receipt
RETURN
END
EXEC sp_OAMethod @receipt, 'StringOfEquals', @iTmp0 OUT, 'destinationFolderId', @destinationFolderId, 1
IF @iTmp0 <> 1
BEGIN
PRINT 'Receipt belongs to another copy operation; 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 @endpoint
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @receipt
RETURN
END
EXEC sp_OAMethod @receipt, 'StringOfEquals', @iTmp0 OUT, 'name', @newName, 1
IF @iTmp0 <> 1
BEGIN
PRINT 'Receipt belongs to another copy operation; 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 @endpoint
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @receipt
RETURN
END
EXEC sp_OAMethod @receipt, 'StringOfEquals', @iTmp0 OUT, 'status', 'completed', 1
IF @iTmp0 = 1
BEGIN
EXEC sp_OAMethod @receipt, 'StringOf', @sTmp0 OUT, 'resourceId'
PRINT 'This copy already completed. Item ID: ' + @sTmp0
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @receipt
RETURN
END
EXEC sp_OAMethod @receipt, 'StringOfEquals', @iTmp0 OUT, 'status', 'failed', 1
IF @iTmp0 = 1
BEGIN
PRINT 'The saved copy failed. Review its error and destination before starting another operation.'
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @receipt
RETURN
END
EXEC sp_OAMethod @receipt, 'StringOfEquals', @iTmp0 OUT, 'status', 'monitoring', 1
IF @iTmp0 <> 1
BEGIN
PRINT 'Copy outcome is uncertain; reconcile the destination before replacing this receipt.'
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @receipt
RETURN
END
EXEC sp_OAMethod @receipt, 'JsonTypeOf', @iTmp0 OUT, 'monitorUrl'
EXEC sp_OAMethod @receipt, 'StringOfEquals', @iTmp1 OUT, 'monitorUrl', '', 1
IF (@iTmp0 <> 1) or (@iTmp1 = 1)
BEGIN
PRINT 'Expected a nonempty monitorUrl 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 @endpoint
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @receipt
RETURN
END
END
ELSE
BEGIN
EXEC sp_OAMethod @receipt, 'UpdateString', @success OUT, 'accountKey', @accountKey
EXEC sp_OAMethod @receipt, 'UpdateString', @success OUT, 'sourceDriveId', @driveId
EXEC sp_OAMethod @receipt, 'UpdateString', @success OUT, 'sourceItemId', @itemId
EXEC sp_OAMethod @receipt, 'UpdateString', @success OUT, 'destinationDriveId', @destinationDriveId
EXEC sp_OAMethod @receipt, 'UpdateString', @success OUT, 'destinationFolderId', @destinationFolderId
EXEC sp_OAMethod @receipt, 'UpdateString', @success OUT, 'name', @newName
-- Save intent before POST so a lost response cannot cause an automatic duplicate copy.
EXEC sp_OAMethod @receipt, 'UpdateString', @success OUT, 'status', 'pending'
EXEC sp_OAMethod @receipt, 'WriteFile', @success OUT, @receiptPath
IF @success <> 1
BEGIN
PRINT 'Could not save the copy intent; nothing submitted.'
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @receipt
RETURN
END
DECLARE @body int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @body OUT
EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'parentReference.driveId', @destinationDriveId
EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'parentReference.id', @destinationFolderId
EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'name', @newName
EXEC sp_OAMethod @endpoint, 'Append', @success OUT, '/copy'
-- Omit conflictBehavior: the default is fail, and the query option is not supported by personal OneDrive.
EXEC sp_OAMethod @endpoint, 'GetAsString', @sTmp0 OUT
EXEC sp_OAMethod @http, 'HttpJson', @success OUT, 'POST', @sTmp0, @body, 'application/json', @resp
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
IF (@success <> 1) and (@iTmp0 = 0)
BEGIN
PRINT 'No HTTP response received; inspect diagnostics privately and reconcile writes 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 @endpoint
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @receipt
EXEC @hr = sp_OADestroy @body
RETURN
END
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
IF (@iTmp0 <> 202)
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 @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @receipt
EXEC @hr = sp_OADestroy @body
RETURN
END
DECLARE @monitorUrl nvarchar(4000)
EXEC sp_OAMethod @resp, 'GetHeaderField', @monitorUrl OUT, 'Location'
IF @monitorUrl = ''
BEGIN
PRINT 'Accepted without a monitor URL; keep the pending receipt and reconcile the destination.'
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @receipt
EXEC @hr = sp_OADestroy @body
RETURN
END
EXEC sp_OAMethod @receipt, 'UpdateString', @success OUT, 'monitorUrl', @monitorUrl
EXEC sp_OAMethod @receipt, 'UpdateString', @success OUT, 'status', 'monitoring'
EXEC sp_OAMethod @receipt, 'WriteFile', @success OUT, @receiptPath
IF @success <> 1
BEGIN
PRINT 'Accepted, but receipt save failed. Do not repeat POST; reconcile the destination.'
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @receipt
EXEC @hr = sp_OADestroy @body
RETURN
END
PRINT 'Copy accepted. Run again later to check its status; honor Retry-After.'
EXEC sp_OAMethod @resp, 'GetHeaderField', @sTmp0 OUT, 'Retry-After'
PRINT 'Retry-After: ' + @sTmp0
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @receipt
EXEC @hr = sp_OADestroy @body
RETURN
END
-- One status request per run. The caller schedules the next run with backoff and Retry-After.
DECLARE @statusUrl nvarchar(4000)
EXEC sp_OAMethod @receipt, 'StringOf', @statusUrl OUT, 'monitorUrl'
DECLARE @serviceUrl int
EXEC @hr = sp_OACreate 'Chilkat.Url', @serviceUrl OUT
EXEC sp_OAMethod @serviceUrl, 'ParseUrl', @success OUT, @statusUrl
IF @success <> 1
BEGIN
PRINT 'Invalid service URL.'
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @receipt
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @serviceUrl
RETURN
END
EXEC sp_OAGetProperty @serviceUrl, 'Ssl', @iTmp0 OUT
EXEC sp_OAGetProperty @serviceUrl, 'Host', @sTmp0 OUT
EXEC sp_OAGetProperty @serviceUrl, 'Login', @sTmp1 OUT
EXEC sp_OAGetProperty @serviceUrl, 'Password', @sTmp2 OUT
IF (@iTmp0 <> 1) or (@sTmp0 = '') or (@sTmp1 <> '') or (@sTmp2 <> '')
BEGIN
PRINT 'Expected an HTTPS service URL without embedded login credentials.'
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @receipt
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @serviceUrl
RETURN
END
DECLARE @monitorHttp int
EXEC @hr = sp_OACreate 'Chilkat.Http', @monitorHttp OUT
EXEC sp_OASetProperty @monitorHttp, 'RequireSslCertVerify', 1
EXEC sp_OASetProperty @monitorHttp, 'FollowRedirects', 0
EXEC sp_OASetProperty @monitorHttp, 'ConnectTimeout', 20
EXEC sp_OASetProperty @monitorHttp, 'ReadTimeout', 120
DECLARE @serviceHost int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @serviceHost OUT
EXEC sp_OAGetProperty @serviceUrl, 'Host', @sTmp0 OUT
EXEC sp_OAMethod @serviceHost, 'Append', @success OUT, @sTmp0
EXEC sp_OAMethod @serviceHost, 'ContentsEqual', @iTmp0 OUT, 'graph.microsoft.com', 0
EXEC sp_OAGetProperty @serviceUrl, 'Port', @iTmp1 OUT
IF (@iTmp0 = 1) and (@iTmp1 = 443)
BEGIN
EXEC sp_OAMethod @token, 'StringOf', @sTmp0 OUT, 'access_token'
EXEC sp_OASetProperty @monitorHttp, 'AuthToken', @sTmp0
END
ELSE
BEGIN
DECLARE @tokenFiles int
EXEC @hr = sp_OACreate 'Chilkat.FileAccess', @tokenFiles OUT
EXEC sp_OAMethod @tokenFiles, 'FileExists', @iTmp0 OUT, 'qa_data/tokens/oneDrive-sharepoint.json'
IF @iTmp0 = 1
BEGIN
DECLARE @sharepointToken int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @sharepointToken OUT
EXEC sp_OASetProperty @sharepointToken, 'Strict', 1
EXEC sp_OAMethod @sharepointToken, 'LoadFile', @success OUT, 'qa_data/tokens/oneDrive-sharepoint.json'
IF @success <> 1
BEGIN
PRINT 'Could not read the private SharePoint token 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 @endpoint
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @receipt
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @serviceUrl
EXEC @hr = sp_OADestroy @monitorHttp
EXEC @hr = sp_OADestroy @serviceHost
EXEC @hr = sp_OADestroy @tokenFiles
EXEC @hr = sp_OADestroy @sharepointToken
RETURN
END
EXEC sp_OAMethod @sharepointToken, 'JsonTypeOf', @iTmp0 OUT, 'host'
EXEC sp_OAMethod @sharepointToken, 'StringOfEquals', @iTmp1 OUT, 'host', '', 1
IF (@iTmp0 <> 1) or (@iTmp1 = 1)
BEGIN
PRINT 'Expected a nonempty host 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 @endpoint
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @receipt
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @serviceUrl
EXEC @hr = sp_OADestroy @monitorHttp
EXEC @hr = sp_OADestroy @serviceHost
EXEC @hr = sp_OADestroy @tokenFiles
EXEC @hr = sp_OADestroy @sharepointToken
RETURN
END
EXEC sp_OAMethod @sharepointToken, 'JsonTypeOf', @iTmp0 OUT, 'access_token'
EXEC sp_OAMethod @sharepointToken, '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
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @receipt
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @serviceUrl
EXEC @hr = sp_OADestroy @monitorHttp
EXEC @hr = sp_OADestroy @serviceHost
EXEC @hr = sp_OADestroy @tokenFiles
EXEC @hr = sp_OADestroy @sharepointToken
RETURN
END
-- host is a configured exact hostname, not a URL and not obtained from the service response.
EXEC sp_OAMethod @sharepointToken, 'StringOf', @sTmp0 OUT, 'host'
EXEC sp_OAMethod @serviceHost, 'ContentsEqual', @iTmp0 OUT, @sTmp0, 0
EXEC sp_OAGetProperty @serviceUrl, 'Port', @iTmp1 OUT
IF (@iTmp0 = 1) and (@iTmp1 = 443)
BEGIN
EXEC sp_OAMethod @sharepointToken, 'StringOf', @sTmp0 OUT, 'access_token'
EXEC sp_OASetProperty @monitorHttp, 'AuthToken', @sTmp0
END
END
END
-- With no matching token configured, only a still-valid preauthenticated URL can succeed.
EXEC sp_OAMethod @monitorHttp, 'HttpNoBody', @success OUT, 'GET', @statusUrl, @resp
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
IF (@success <> 1) and (@iTmp0 = 0)
BEGIN
PRINT 'No HTTP response received; inspect diagnostics privately and reconcile writes 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 @endpoint
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @receipt
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @serviceUrl
EXEC @hr = sp_OADestroy @monitorHttp
EXEC @hr = sp_OADestroy @serviceHost
EXEC @hr = sp_OADestroy @tokenFiles
EXEC @hr = sp_OADestroy @sharepointToken
RETURN
END
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp1 OUT
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp2 OUT
IF (@iTmp0 <> 200) and (@iTmp1 <> 202) and (@iTmp2 <> 303)
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 @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @receipt
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @serviceUrl
EXEC @hr = sp_OADestroy @monitorHttp
EXEC @hr = sp_OADestroy @serviceHost
EXEC @hr = sp_OADestroy @tokenFiles
EXEC @hr = sp_OADestroy @sharepointToken
RETURN
END
DECLARE @completed int
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
SELECT @completed = @iTmp0 = 303
EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
IF @sTmp0 <> ''
BEGIN
EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
EXEC sp_OAMethod @result, 'Load', @success OUT, @sTmp0
IF @success <> 1
BEGIN
PRINT 'Invalid monitor JSON; keep the receipt and inspect the operation.'
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @receipt
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @serviceUrl
EXEC @hr = sp_OADestroy @monitorHttp
EXEC @hr = sp_OADestroy @serviceHost
EXEC @hr = sp_OADestroy @tokenFiles
EXEC @hr = sp_OADestroy @sharepointToken
RETURN
END
EXEC sp_OAMethod @result, 'StringOfEquals', @iTmp0 OUT, 'status', 'completed', 0
IF @iTmp0 = 1
BEGIN
SELECT @completed = 1
END
EXEC sp_OAMethod @result, 'StringOfEquals', @iTmp0 OUT, 'status', 'failed', 0
IF @iTmp0 = 1
BEGIN
EXEC sp_OAMethod @receipt, 'UpdateString', @success OUT, 'status', 'failed'
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'error.code'
EXEC sp_OAMethod @receipt, 'UpdateString', @success OUT, 'errorCode', @sTmp0
EXEC sp_OAMethod @receipt, 'WriteFile', @success OUT, @receiptPath
IF @success <> 1
BEGIN
PRINT 'Could not save the failed-operation receipt.'
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @receipt
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @serviceUrl
EXEC @hr = sp_OADestroy @monitorHttp
EXEC @hr = sp_OADestroy @serviceHost
EXEC @hr = sp_OADestroy @tokenFiles
EXEC @hr = sp_OADestroy @sharepointToken
RETURN
END
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'error.code'
PRINT 'Copy failed: ' + @sTmp0
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @receipt
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @serviceUrl
EXEC @hr = sp_OADestroy @monitorHttp
EXEC @hr = sp_OADestroy @serviceHost
EXEC @hr = sp_OADestroy @tokenFiles
EXEC @hr = sp_OADestroy @sharepointToken
RETURN
END
END
IF @completed = 1
BEGIN
EXEC sp_OAMethod @receipt, 'UpdateString', @success OUT, 'status', 'completed'
EXEC sp_OAMethod @result, 'JsonTypeOf', @iTmp0 OUT, 'resourceId'
IF @iTmp0 = 1
BEGIN
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'resourceId'
EXEC sp_OAMethod @receipt, 'UpdateString', @success OUT, 'resourceId', @sTmp0
END
EXEC sp_OAMethod @receipt, 'WriteFile', @success OUT, @receiptPath
IF @success <> 1
BEGIN
PRINT 'Copy completed but the receipt could not be saved. Reconcile before another copy.'
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @receipt
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @serviceUrl
EXEC @hr = sp_OADestroy @monitorHttp
EXEC @hr = sp_OADestroy @serviceHost
EXEC @hr = sp_OADestroy @tokenFiles
EXEC @hr = sp_OADestroy @sharepointToken
RETURN
END
PRINT 'Copy completed. Destination drive ID: ' + @destinationDriveId
EXEC sp_OAMethod @receipt, 'StringOf', @sTmp0 OUT, 'resourceId'
PRINT 'New item ID, if returned: ' + @sTmp0
-- A 303 without a JSON resourceId still indicates completion. Look up the destination by name if needed.
-- Do not follow Location/resourceLocation with the Graph bearer token automatically.
END
ELSE
BEGIN
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'status'
PRINT 'Copy not completed. Status: ' + @sTmp0
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'percentageComplete'
PRINT 'Percent complete, if returned: ' + @sTmp0
EXEC sp_OAMethod @resp, 'GetHeaderField', @sTmp0 OUT, 'Retry-After'
PRINT 'Retry-After: ' + @sTmp0
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 @endpoint
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @receipt
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @serviceUrl
EXEC @hr = sp_OADestroy @monitorHttp
EXEC @hr = sp_OADestroy @serviceHost
EXEC @hr = sp_OADestroy @tokenFiles
EXEC @hr = sp_OADestroy @sharepointToken
END
GO