OneDrive: List Available Drives with Pagination
See more OneDrive Examples
List drives available for the signed-in user via /me/drives. A drive is a personal OneDrive, a business OneDrive, or a SharePoint document library. Save its ID for the other examples. This is not a tenant-wide inventory. For a specific organizational user, group, or site use its /drives relationship with the appropriate authorization. Quota is optional and values can exceed int.
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.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 PureBasic Downloads
IncludeFile "CkHttpResponse.pb"
IncludeFile "CkHttp.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkHashtable.pb"
Procedure ChilkatExample()
success.i = 0
; Assumes Chilkat is unlocked. See Global Unlock Sample.
success = 0
; Obtain a delegated Graph token with this example's file permissions.
; See OneDrive OAuth2 with PKCE.
token.i = CkJsonObject::ckCreate()
If token.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkJsonObject::setCkStrict(token, 1)
success = CkJsonObject::ckLoadFile(token,"qa_data/tokens/oneDrive.json")
If success <> 1
Debug "Could not load the private OneDrive token cache."
CkJsonObject::ckDispose(token)
ProcedureReturn
EndIf
If (CkJsonObject::ckJsonTypeOf(token,"access_token") <> 1) OR (CkJsonObject::ckStringOfEquals(token,"access_token","",1) = 1)
Debug "Expected a nonempty access_token in the JSON."
CkJsonObject::ckDispose(token)
ProcedureReturn
EndIf
http.i = CkHttp::ckCreate()
If http.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkHttp::setCkAuthToken(http, CkJsonObject::ckStringOf(token,"access_token"))
CkHttp::setCkRequireSslCertVerify(http, 1)
CkHttp::setCkFollowRedirects(http, 0)
CkHttp::setCkConnectTimeout(http, 20)
CkHttp::setCkReadTimeout(http, 120)
CkHttp::ckSetRequestHeader(http,"Accept","application/json")
resp.i = CkHttpResponse::ckCreate()
If resp.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
result.i = CkJsonObject::ckCreate()
If result.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkJsonObject::setCkStrict(result, 1)
url.s = "https://graph.microsoft.com/v1.0/me/drives?$select=id,name,driveType,quota&$top=50"
nextLink.i = CkStringBuilder::ckCreate()
If nextLink.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
seenLinks.i = CkHashtable::ckCreate()
If seenLinks.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
pageCount.i = 0
While ((url <> "") AND (pageCount < 100))
CkStringBuilder::ckSetString(nextLink,url)
If CkStringBuilder::ckStartsWith(nextLink,"https://graph.microsoft.com/v1.0/",1) <> 1
Debug "Unexpected pagination host or API version; no request sent."
CkJsonObject::ckDispose(token)
CkHttp::ckDispose(http)
CkHttpResponse::ckDispose(resp)
CkJsonObject::ckDispose(result)
CkStringBuilder::ckDispose(nextLink)
CkHashtable::ckDispose(seenLinks)
ProcedureReturn
EndIf
If CkHashtable::ckContains(seenLinks,url) = 1
Debug "Repeated pagination URL; stopping."
CkJsonObject::ckDispose(token)
CkHttp::ckDispose(http)
CkHttpResponse::ckDispose(resp)
CkJsonObject::ckDispose(result)
CkStringBuilder::ckDispose(nextLink)
CkHashtable::ckDispose(seenLinks)
ProcedureReturn
EndIf
CkHashtable::ckAddInt(seenLinks,url,1)
success = CkHttp::ckHttpNoBody(http,"GET",url,resp)
If (success <> 1) AND (CkHttpResponse::ckStatusCode(resp) = 0)
Debug "No HTTP response received; inspect diagnostics privately and reconcile writes before retrying."
CkJsonObject::ckDispose(token)
CkHttp::ckDispose(http)
CkHttpResponse::ckDispose(resp)
CkJsonObject::ckDispose(result)
CkStringBuilder::ckDispose(nextLink)
CkHashtable::ckDispose(seenLinks)
ProcedureReturn
EndIf
If (CkHttpResponse::ckStatusCode(resp) <> 200)
Debug "Unexpected HTTP status: " + Str(CkHttpResponse::ckStatusCode(resp))
Debug "Request ID: " + CkHttpResponse::ckGetHeaderField(resp,"request-id")
If CkHttpResponse::ckStatusCode(resp) = 429
Debug "Retry-After: " + CkHttpResponse::ckGetHeaderField(resp,"Retry-After")
EndIf
CkJsonObject::ckDispose(token)
CkHttp::ckDispose(http)
CkHttpResponse::ckDispose(resp)
CkJsonObject::ckDispose(result)
CkStringBuilder::ckDispose(nextLink)
CkHashtable::ckDispose(seenLinks)
ProcedureReturn
EndIf
success = CkJsonObject::ckLoad(result,CkHttpResponse::ckBodyStr(resp))
If success <> 1
Debug "Expected a well-formed JSON response."
CkJsonObject::ckDispose(token)
CkHttp::ckDispose(http)
CkHttpResponse::ckDispose(resp)
CkJsonObject::ckDispose(result)
CkStringBuilder::ckDispose(nextLink)
CkHashtable::ckDispose(seenLinks)
ProcedureReturn
EndIf
If CkJsonObject::ckJsonTypeOf(result,"value") <> 4
Debug "Expected a value array."
CkJsonObject::ckDispose(token)
CkHttp::ckDispose(http)
CkHttpResponse::ckDispose(resp)
CkJsonObject::ckDispose(result)
CkStringBuilder::ckDispose(nextLink)
CkHashtable::ckDispose(seenLinks)
ProcedureReturn
EndIf
i.i = 0
While (i < CkJsonObject::ckSizeOfArray(result,"value"))
CkJsonObject::setCkI(result, i)
If (CkJsonObject::ckJsonTypeOf(result,"value[i].id") <> 1) OR (CkJsonObject::ckStringOfEquals(result,"value[i].id","",1) = 1)
Debug "Expected a nonempty value[i].id in the JSON."
CkJsonObject::ckDispose(token)
CkHttp::ckDispose(http)
CkHttpResponse::ckDispose(resp)
CkJsonObject::ckDispose(result)
CkStringBuilder::ckDispose(nextLink)
CkHashtable::ckDispose(seenLinks)
ProcedureReturn
EndIf
Debug "Drive ID: " + CkJsonObject::ckStringOf(result,"value[i].id")
Debug "Name: " + CkJsonObject::ckStringOf(result,"value[i].name")
Debug "Drive type: " + CkJsonObject::ckStringOf(result,"value[i].driveType")
; Quota values can be larger than int. Missing quota fields are not zero.
If CkJsonObject::ckHasMember(result,"value[i].quota.total") = 1
Debug "Total bytes: " + CkJsonObject::ckStringOf(result,"value[i].quota.total")
Debug "Remaining bytes: " + CkJsonObject::ckStringOf(result,"value[i].quota.remaining")
EndIf
i = i + 1
Wend
pageCount = pageCount + 1
; A quoted JSON path treats the period in @odata.nextLink literally.
nextType.i = CkJsonObject::ckJsonTypeOf(result,Chr(34) + "@odata.nextLink" + Chr(34))
If (nextType = -1) OR (nextType = 6)
url = ""
Else
If (nextType <> 1) OR (CkJsonObject::ckStringOfEquals(result,Chr(34) + "@odata.nextLink" + Chr(34),"",1) = 1)
Debug "Invalid continuation link; cannot establish completeness."
CkJsonObject::ckDispose(token)
CkHttp::ckDispose(http)
CkHttpResponse::ckDispose(resp)
CkJsonObject::ckDispose(result)
CkStringBuilder::ckDispose(nextLink)
CkHashtable::ckDispose(seenLinks)
ProcedureReturn
EndIf
url = CkJsonObject::ckStringOf(result,Chr(34) + "@odata.nextLink" + Chr(34))
EndIf
Wend
If url <> ""
Debug "Stopped at the example's 100-page limit; save the continuation privately to resume."
CkJsonObject::ckDispose(token)
CkHttp::ckDispose(http)
CkHttpResponse::ckDispose(resp)
CkJsonObject::ckDispose(result)
CkStringBuilder::ckDispose(nextLink)
CkHashtable::ckDispose(seenLinks)
ProcedureReturn
EndIf
Debug "Completed pages: " + Str(pageCount)
CkJsonObject::ckDispose(token)
CkHttp::ckDispose(http)
CkHttpResponse::ckDispose(resp)
CkJsonObject::ckDispose(result)
CkStringBuilder::ckDispose(nextLink)
CkHashtable::ckDispose(seenLinks)
ProcedureReturn
EndProcedure