OneDrive: Share a File or Folder with Specific Recipients
See more OneDrive Examples
Changes access: grant read access to selected recipients without creating an anonymous link. Replace both sample addresses, or remove the second recipient for a single grant. requireSignIn=true requires authentication, retainInheritedPermissions=true preserves inherited access, and sendInvitation=false avoids sending email. Changing sendInvitation to true sends invitation messages.
Sharing is subject to the owner's rights, tenant policies, and guest restrictions. New guests cannot be invited with app-only access; existing guests can receive grants. Personal OneDrive does not permit sharing permissions on its root through this action.
Handle both 200 and 207. Each result must be inspected, and an error can concern invitation delivery after the permission was created. Retain permission IDs and reconcile the current access before retrying only the failed work. A successful permission response does not prove email delivery.
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 @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'
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 @body int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @body OUT
EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'recipients[0].email', 'first-recipient@example.com'
EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'recipients[1].email', 'second-recipient@example.com'
EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'roles[0]', 'read'
EXEC sp_OAMethod @body, 'UpdateBool', @success OUT, 'requireSignIn', 1
-- false grants access without an invitation email. Set true deliberately to notify recipients.
EXEC sp_OAMethod @body, 'UpdateBool', @success OUT, 'sendInvitation', 0
EXEC sp_OAMethod @body, 'UpdateBool', @success OUT, 'retainInheritedPermissions', 1
EXEC sp_OAMethod @endpoint, 'Append', @success OUT, '/invite'
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 @body
RETURN
END
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp1 OUT
IF (@iTmp0 <> 200) and (@iTmp1 <> 207)
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 @body
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 @endpoint
EXEC @hr = sp_OADestroy @body
RETURN
END
EXEC sp_OAMethod @result, 'JsonTypeOf', @iTmp0 OUT, 'value'
IF @iTmp0 <> 4
BEGIN
PRINT 'Expected individual permission results.'
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 @body
RETURN
END
DECLARE @hasErrors int
SELECT @hasErrors = 0
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'
IF @iTmp0 = 1
BEGIN
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'value[i].id'
PRINT 'Permission ID: ' + @sTmp0
END
EXEC sp_OAMethod @result, 'JsonTypeOf', @iTmp0 OUT, 'value[i].error'
IF @iTmp0 = 3
BEGIN
SELECT @hasErrors = 1
PRINT 'Result index: ' + @i
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'value[i].error.code'
PRINT 'Error code: ' + @sTmp0
-- Notification can fail even after access was granted. Inspect current permissions before retrying.
END
ELSE
BEGIN
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 @endpoint
EXEC @hr = sp_OADestroy @body
RETURN
END
PRINT 'Permission returned for this result.'
END
SELECT @i = @i + 1
END
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
IF (@iTmp0 = 207) or (@hasErrors = 1)
BEGIN
PRINT 'Partial outcome: review each result and current permissions. Do not resend the entire batch.'
END
ELSE
BEGIN
PRINT 'Sharing request completed; inspect the returned grants.'
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 @body
END
GO