Sample code for 30+ languages & platforms
SQL Server Requires Chilkat v11.6.0+

OneDrive: Inspect Sharing Permissions and Revoke a Grant

See more OneDrive Examples

List an item's sharing permissions. Owners can see more grants than nonowners; this is not necessarily a complete access audit. Roles, link metadata, and inheritedFrom are optional. OneDrive for Business and SharePoint do not consistently expose inheritedFrom, so its absence does not prove the grant can be deleted.

Optional access change: the default permissionId is empty and only inspects. Set a specific permission ID to revoke it after reviewing the listing. The DELETE API removes only directly assigned permissions, not inherited access. Known inherited grants are blocked locally, and the service decides other cases. Removing a grant does not remove access supplied through other links, groups, or parent permissions. Private sharing URLs are not printed.

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.

Permission: Files.ReadWrite for revocation; Files.Read for inspection only.

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.

Microsoft documentation

Chilkat SQL Server Downloads

SQL Server
-- 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
    --  Empty means inspect only. Set a specific permission ID to revoke that grant.
    DECLARE @permissionId nvarchar(4000)
    SELECT @permissionId = ''
    DECLARE @foundPermission int
    SELECT @foundPermission = 0
    DECLARE @inheritedPermission int
    SELECT @inheritedPermission = 0
    EXEC sp_OAMethod @endpoint, 'Append', @success OUT, '/permissions'
    DECLARE @url nvarchar(4000)
    EXEC sp_OAMethod @endpoint, 'GetAsString', @url OUT
    DECLARE @nextLink int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @nextLink OUT

    DECLARE @seenLinks int
    EXEC @hr = sp_OACreate 'Chilkat.Hashtable', @seenLinks OUT

    DECLARE @pageCount int
    SELECT @pageCount = 0
    WHILE ((@url <> '') and (@pageCount < 100))
      BEGIN
        EXEC sp_OAMethod @nextLink, 'SetString', @success OUT, @url
        EXEC sp_OAMethod @nextLink, 'StartsWith', @iTmp0 OUT, 'https://graph.microsoft.com/v1.0/', 1
        IF @iTmp0 <> 1
          BEGIN

            PRINT 'Unexpected pagination 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 @endpoint
            EXEC @hr = sp_OADestroy @nextLink
            EXEC @hr = sp_OADestroy @seenLinks
            RETURN
          END
        EXEC sp_OAMethod @seenLinks, 'Contains', @iTmp0 OUT, @url
        IF @iTmp0 = 1
          BEGIN

            PRINT 'Repeated pagination 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 @endpoint
            EXEC @hr = sp_OADestroy @nextLink
            EXEC @hr = sp_OADestroy @seenLinks
            RETURN
          END
        EXEC sp_OAMethod @seenLinks, 'AddInt', @success OUT, @url, 1
        EXEC sp_OAMethod @http, 'HttpNoBody', @success OUT, 'GET', @url, @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 @nextLink
            EXEC @hr = sp_OADestroy @seenLinks
            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 @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 @nextLink
            EXEC @hr = sp_OADestroy @seenLinks
            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 @nextLink
            EXEC @hr = sp_OADestroy @seenLinks
            RETURN
          END
        EXEC sp_OAMethod @result, 'JsonTypeOf', @iTmp0 OUT, 'value'
        IF @iTmp0 <> 4
          BEGIN

            PRINT 'Expected a value array.'
            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 @nextLink
            EXEC @hr = sp_OADestroy @seenLinks
            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 @endpoint
                EXEC @hr = sp_OADestroy @nextLink
                EXEC @hr = sp_OADestroy @seenLinks
                RETURN
              END

            EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'value[i].id'
            PRINT 'Permission ID: ' + @sTmp0
            DECLARE @roleIndex int
            SELECT @roleIndex = 0
            EXEC sp_OAMethod @result, 'SizeOfArray', @iTmp0 OUT, 'value[i].roles'
            WHILE (@roleIndex < @iTmp0)
              BEGIN
                EXEC sp_OASetProperty @result, 'J', @roleIndex

                EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'value[i].roles[j]'
                PRINT 'Role: ' + @sTmp0
                SELECT @roleIndex = @roleIndex + 1
              END
            EXEC sp_OAMethod @result, 'JsonTypeOf', @iTmp0 OUT, 'value[i].link'
            IF @iTmp0 = 3
              BEGIN

                EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'value[i].link.type'
                PRINT 'Link type: ' + @sTmp0

                EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'value[i].link.scope'
                PRINT 'Link scope: ' + @sTmp0
              END
            EXEC sp_OAMethod @result, 'JsonTypeOf', @iTmp0 OUT, 'value[i].inheritedFrom'
            IF @iTmp0 = 3
              BEGIN

                PRINT 'Inherited from a parent; cannot revoke here.'
              END
            EXEC sp_OAMethod @result, 'StringOfEquals', @iTmp0 OUT, 'value[i].id', @permissionId, 1
            IF @iTmp0 = 1
              BEGIN
                SELECT @foundPermission = 1
                EXEC sp_OAMethod @result, 'JsonTypeOf', @iTmp0 OUT, 'value[i].inheritedFrom'
                SELECT @inheritedPermission = @iTmp0 = 3
              END
            SELECT @i = @i + 1
          END
        SELECT @pageCount = @pageCount + 1
        --  A quoted JSON path treats the period in @odata.nextLink literally.
        DECLARE @nextType int
        EXEC sp_OAMethod @result, 'JsonTypeOf', @nextType OUT, '"@odata.nextLink"'
        IF (@nextType = -1) or (@nextType = 6)
          BEGIN
            SELECT @url = ''
          END
        ELSE
          BEGIN
            EXEC sp_OAMethod @result, 'StringOfEquals', @iTmp0 OUT, '"@odata.nextLink"', '', 1
            IF (@nextType <> 1) or (@iTmp0 = 1)
              BEGIN

                PRINT 'Invalid continuation link; cannot establish completeness.'
                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 @nextLink
                EXEC @hr = sp_OADestroy @seenLinks
                RETURN
              END
            EXEC sp_OAMethod @result, 'StringOf', @url OUT, '"@odata.nextLink"'
          END
      END
    IF @url <> ''
      BEGIN

        PRINT 'Stopped at the example''s 100-page limit; save the continuation privately to resume.'
        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 @nextLink
        EXEC @hr = sp_OADestroy @seenLinks
        RETURN
      END

    PRINT 'Completed pages: ' + @pageCount
    IF @permissionId = ''
      BEGIN

        PRINT 'Inspection only; no access changed.'
        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 @nextLink
        EXEC @hr = sp_OADestroy @seenLinks
        RETURN
      END
    IF @foundPermission <> 1
      BEGIN

        PRINT 'Selected permission was not in the completed listing; no deletion 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 @endpoint
        EXEC @hr = sp_OADestroy @nextLink
        EXEC @hr = sp_OADestroy @seenLinks
        RETURN
      END
    IF @inheritedPermission = 1
      BEGIN

        PRINT 'This permission is inherited; manage it at its parent instead.'
        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 @nextLink
        EXEC @hr = sp_OADestroy @seenLinks
        RETURN
      END
    --  Missing inheritedFrom is not proof of a directly removable permission on every service.
    --  The server enforces whether the selected grant can be deleted.
    EXEC sp_OAMethod @endpoint, 'Append', @success OUT, '/'
    EXEC sp_OAMethod @http, 'UrlEncode', @sTmp0 OUT, @permissionId
    EXEC sp_OAMethod @endpoint, 'Append', @success OUT, @sTmp0
    EXEC sp_OAMethod @endpoint, 'GetAsString', @sTmp0 OUT
    EXEC sp_OAMethod @http, 'HttpNoBody', @success OUT, 'DELETE', @sTmp0, @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 @nextLink
        EXEC @hr = sp_OADestroy @seenLinks
        RETURN
      END
    EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
    IF (@iTmp0 <> 204)
      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 @nextLink
        EXEC @hr = sp_OADestroy @seenLinks
        RETURN
      END

    PRINT 'Selected permission revoked. Other grants can still provide access.'

    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 @nextLink
    EXEC @hr = sp_OADestroy @seenLinks


END
GO