Sample code for 30+ languages & platforms
PureBasic

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 PureBasic Downloads

PureBasic
IncludeFile "CkHttp.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkDateTime.pb"

Procedure ChilkatExample()

    success.i = 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
    json.i = CkJsonObject::ckCreate()
    If json.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckUpdateString(json,"client_id","2871da2c-8176-4b7f-869b-2311aa82e743")
    CkJsonObject::ckUpdateString(json,"client_secret","2hu9Q~r5QuryUcEkNbg1btLtnfU1VUXzhSCG6brH")
    CkJsonObject::ckUpdateString(json,"scope","https://graph.microsoft.com/.default")
    CkJsonObject::ckUpdateString(json,"token_endpoint","https://login.microsoftonline.com/114d7ed6-71bf-4dbe-a866-748364121bf2/oauth2/v2.0/token")

    http.i = CkHttp::ckCreate()
    If http.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkHttp::setCkAuthToken(http, CkJsonObject::ckEmit(json))

    ; 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.)
    CkHttp::ckSetUrlVar(http,"item_path","/TestDir")
    CkHttp::ckSetUrlVar(http,"user_id","4fe732c3-322e-4a6b-b729-2fd1eb5c6104")

    resp.s = CkHttp::ckQuickGetStr(http,"https://graph.microsoft.com/v1.0/users/{$user_id}/drive/root:{$item_path}:/children")
    If CkHttp::ckLastMethodSuccess(http) <> 1
        Debug CkHttp::ckLastErrorText(http)
        CkJsonObject::ckDispose(json)
        CkHttp::ckDispose(http)
        ProcedureReturn
    EndIf

    ; The response should be JSON.
    CkJsonObject::setCkEmitCompact(json, 0)
    CkJsonObject::ckLoad(json,resp)

    ; A successful response should return a status code of 200.
    If CkHttp::ckLastStatus(http) <> 200
        Debug CkJsonObject::ckEmit(json)
        Debug "Response status = " + Str(CkHttp::ckLastStatus(http))
        CkJsonObject::ckDispose(json)
        CkHttp::ckDispose(http)
        ProcedureReturn
    EndIf

    Debug CkJsonObject::ckEmit(json)

    lastMod.i = CkDateTime::ckCreate()
    If lastMod.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    photoTaken.i = CkDateTime::ckCreate()
    If photoTaken.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ; Iterate over the DriveItems in the JSON response:
    i.i = 0
    numItems.i = CkJsonObject::ckSizeOfArray(json,"value")
    While i < numItems
        CkJsonObject::setCkI(json, i)
        Debug "-- DriveItem " + Str(i + 1)
        Debug "id: " + CkJsonObject::ckStringOf(json,"value[i].id")
        Debug "name: " + CkJsonObject::ckStringOf(json,"value[i].name")
        Debug "size: " + Str(CkJsonObject::ckIntOf(json,"value[i].size"))

        ; Get the lastModifiedDateTime
        CkDateTime::ckSetFromTimestamp(lastMod,CkJsonObject::ckStringOf(json,"value[i].fileSystemInfo.lastModifiedDateTime"))

        ; Is this a folder?
        If CkJsonObject::ckHasMember(json,"value[i].folder") = 1
            Debug "This is a folder with " + Str(CkJsonObject::ckIntOf(json,"value[i].folder.childCount")) + " children"
        EndIf

        If CkJsonObject::ckHasMember(json,"value[i].file") = 1
            Debug "This is a file."
            Debug "SHA1 hash: " + CkJsonObject::ckStringOf(json,"value[i].file.hashes.sha1Hash")
            Debug "mimeType: " + CkJsonObject::ckStringOf(json,"value[i].mimeType")
        EndIf

        If CkJsonObject::ckHasMember(json,"value[i].image") = 1
            Debug "This is an image."
            Debug "height: " + Str(CkJsonObject::ckIntOf(json,"value[i].image.height"))
            Debug "width: " + Str(CkJsonObject::ckIntOf(json,"value[i].image.width"))
        EndIf

        If CkJsonObject::ckHasMember(json,"value[i].photo") = 1
            Debug "This is a photo."
            CkDateTime::ckSetFromTimestamp(photoTaken,CkJsonObject::ckStringOf(json,"value[i].photo.takenDateTime"))
            Debug "photo taken on " + CkDateTime::ckGetAsRfc822(photoTaken,1)
        EndIf

        If CkJsonObject::ckHasMember(json,"value[i].audio") = 1
            Debug "This is an audio file."
            Debug "duration: " + Str(CkJsonObject::ckIntOf(json,"value[i].audio.duration"))
        EndIf

        i = i + 1
    Wend


    CkJsonObject::ckDispose(json)
    CkHttp::ckDispose(http)
    CkDateTime::ckDispose(lastMod)
    CkDateTime::ckDispose(photoTaken)


    ProcedureReturn
EndProcedure