Xbase++
Xbase++
OneDrive -- List Non-Root Directory
See more OneDrive Examples
This gets the collection of DriveItem children for a non-root DriveItem. This is the same as for getting the children for the root DriveItem, except the URL includes the path to the desired non-root DriveItem.Note: This example requires Chilkat v9.5.0.97 or greater.
Chilkat Xbase++ Downloads
LOCAL nSuccess
LOCAL oJson
LOCAL oHttp
LOCAL cResp
LOCAL oLastMod
LOCAL oPhotoTaken
LOCAL i
LOCAL nNumItems
nSuccess := 0
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// This example uses the OAuth client credentials flow.
// See How to Create an Azure App Registration for OAuth 2.0 Client Credentials
// Use your client ID, client secret, and tenant ID in the following lines
oJson := CreateObject("Chilkat.JsonObject")
oJson:UpdateString("client_id", "2871da2c-8176-4b7f-869b-2311aa82e743")
oJson:UpdateString("client_secret", "2hu9Q~r5QuryUcEkNbg1btLtnfU1VUXzhSCG6brH")
oJson:UpdateString("scope", "https://graph.microsoft.com/.default")
oJson:UpdateString("token_endpoint", "https://login.microsoftonline.com/114d7ed6-71bf-4dbe-a866-748364121bf2/oauth2/v2.0/token")
oHttp := CreateObject("Chilkat.Http")
oHttp:AuthToken := oJson:Emit()
// Send a Get request like this:
// GET /users/{user-id}/drive/root:/{item-path}:/children
// This example will get the DriveItems in /TestDir
// (In other words, we're getting the directory listing for /TestDir.)
oHttp:SetUrlVar("item_path", "/TestDir")
oHttp:SetUrlVar("user_id", "4fe732c3-322e-4a6b-b729-2fd1eb5c6104")
cResp := oHttp:QuickGetStr("https://graph.microsoft.com/v1.0/users/{$user_id}/drive/root:{$item_path}:/children")
IF (oHttp:LastMethodSuccess != 1)
? oHttp:LastErrorText
oJson:destroy()
oHttp:destroy()
RETURN
ENDIF
// The response should be JSON.
oJson:EmitCompact := 0
oJson:Load(cResp)
// A successful response should return a status code of 200.
IF (oHttp:LastStatus != 200)
? oJson:Emit()
? "Response status = " + Str(oHttp:LastStatus)
oJson:destroy()
oHttp:destroy()
RETURN
ENDIF
? oJson:Emit()
oLastMod := CreateObject("Chilkat.CkDateTime")
oPhotoTaken := CreateObject("Chilkat.CkDateTime")
// Iterate over the DriveItems in the JSON response:
i := 0
nNumItems := oJson:SizeOfArray("value")
DO WHILE i < nNumItems
oJson:I := i
? "-- DriveItem " + Str(i + 1)
? "id: " + oJson:StringOf("value[i].id")
? "name: " + oJson:StringOf("value[i].name")
? "size: " + Str(oJson:IntOf("value[i].size"))
// Get the lastModifiedDateTime
oLastMod:SetFromTimestamp(oJson:StringOf("value[i].fileSystemInfo.lastModifiedDateTime"))
// Is this a folder?
IF (oJson:HasMember("value[i].folder") == 1)
? "This is a folder with " + Str(oJson:IntOf("value[i].folder.childCount")) + " children"
ENDIF
IF (oJson:HasMember("value[i].file") == 1)
? "This is a file."
? "SHA1 hash: " + oJson:StringOf("value[i].file.hashes.sha1Hash")
? "mimeType: " + oJson:StringOf("value[i].mimeType")
ENDIF
IF (oJson:HasMember("value[i].image") == 1)
? "This is an image."
? "height: " + Str(oJson:IntOf("value[i].image.height"))
? "width: " + Str(oJson:IntOf("value[i].image.width"))
ENDIF
IF (oJson:HasMember("value[i].photo") == 1)
? "This is a photo."
oPhotoTaken:SetFromTimestamp(oJson:StringOf("value[i].photo.takenDateTime"))
? "photo taken on " + oPhotoTaken:GetAsRfc822(1)
ENDIF
IF (oJson:HasMember("value[i].audio") == 1)
? "This is an audio file."
? "duration: " + Str(oJson:IntOf("value[i].audio.duration"))
ENDIF
i := i + 1
ENDDO
oJson:destroy()
oHttp:destroy()
oLastMod:destroy()
oPhotoTaken:destroy()