OneDrive: Find a SharePoint Site and List Its Document Libraries
See more OneDrive Examples
Resolve a SharePoint site by hostname and relative path, then enumerate its document libraries as drives. The sample path is sites/ExampleSite; change the managed path and site name, encoding each component separately. A site ID is an opaque composite string, not a GUID to split or a drive ID. Save each returned drive ID for the ordinary OneDrive file examples.
This requires Microsoft 365 SharePoint, not personal OneDrive. The site lookup requires Sites.Read.All, which is not included in the default Files.ReadWrite consent in this collection. Obtain the additional delegated consent before running; application Sites.Read.All requires administrator consent. An appropriately configured selected-access design is another option, but permission configuration and explicit site grants must both be established.
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: Sites.Read.All for site resolution and library listing.
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
-- The site hostname and each path component are known inputs, not drive IDs.
DECLARE @siteHost nvarchar(4000)
SELECT @siteHost = 'YOUR_TENANT.sharepoint.com'
DECLARE @managedPath nvarchar(4000)
SELECT @managedPath = 'sites'
DECLARE @siteName nvarchar(4000)
SELECT @siteName = 'ExampleSite'
DECLARE @siteEndpoint int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @siteEndpoint OUT
EXEC sp_OAMethod @siteEndpoint, 'Append', @success OUT, 'https://graph.microsoft.com/v1.0/sites/'
EXEC sp_OAMethod @http, 'UrlEncode', @sTmp0 OUT, @siteHost
EXEC sp_OAMethod @siteEndpoint, 'Append', @success OUT, @sTmp0
EXEC sp_OAMethod @siteEndpoint, 'Append', @success OUT, ':/'
EXEC sp_OAMethod @http, 'UrlEncode', @sTmp0 OUT, @managedPath
EXEC sp_OAMethod @siteEndpoint, 'Append', @success OUT, @sTmp0
EXEC sp_OAMethod @siteEndpoint, 'Append', @success OUT, '/'
EXEC sp_OAMethod @http, 'UrlEncode', @sTmp0 OUT, @siteName
EXEC sp_OAMethod @siteEndpoint, 'Append', @success OUT, @sTmp0
EXEC sp_OAMethod @siteEndpoint, 'Append', @success OUT, '?$select=id,displayName,webUrl'
EXEC sp_OAMethod @siteEndpoint, 'GetAsString', @sTmp0 OUT
EXEC sp_OAMethod @http, 'HttpNoBody', @success OUT, 'GET', @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 @siteEndpoint
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 @siteEndpoint
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 @siteEndpoint
RETURN
END
EXEC sp_OAMethod @result, 'JsonTypeOf', @iTmp0 OUT, 'id'
EXEC sp_OAMethod @result, 'StringOfEquals', @iTmp1 OUT, 'id', '', 1
IF (@iTmp0 <> 1) or (@iTmp1 = 1)
BEGIN
PRINT 'Expected a nonempty 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 @siteEndpoint
RETURN
END
DECLARE @siteId nvarchar(4000)
EXEC sp_OAMethod @result, 'StringOf', @siteId OUT, 'id'
PRINT 'Site ID: ' + @siteId
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'displayName'
PRINT 'Site name: ' + @sTmp0
DECLARE @endpoint int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @endpoint OUT
EXEC sp_OAMethod @endpoint, 'Append', @success OUT, 'https://graph.microsoft.com/v1.0/sites/'
EXEC sp_OAMethod @http, 'UrlEncode', @sTmp0 OUT, @siteId
EXEC sp_OAMethod @endpoint, 'Append', @success OUT, @sTmp0
EXEC sp_OAMethod @endpoint, 'Append', @success OUT, '/drives?$select=id,name,driveType,quota&$top=50'
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 @siteEndpoint
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 @siteEndpoint
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 @siteEndpoint
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 @siteEndpoint
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 @siteEndpoint
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 @siteEndpoint
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 @siteEndpoint
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 'Drive ID: ' + @sTmp0
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'value[i].name'
PRINT 'Name: ' + @sTmp0
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'value[i].driveType'
PRINT 'Drive type: ' + @sTmp0
-- Quota values can be larger than int. Missing quota fields are not zero.
EXEC sp_OAMethod @result, 'HasMember', @iTmp0 OUT, 'value[i].quota.total'
IF @iTmp0 = 1
BEGIN
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'value[i].quota.total'
PRINT 'Total bytes: ' + @sTmp0
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'value[i].quota.remaining'
PRINT 'Remaining bytes: ' + @sTmp0
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 @siteEndpoint
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 @siteEndpoint
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @nextLink
EXEC @hr = sp_OADestroy @seenLinks
RETURN
END
PRINT 'Completed pages: ' + @pageCount
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @siteEndpoint
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @nextLink
EXEC @hr = sp_OADestroy @seenLinks
END
GO