Sample code for 30+ languages & platforms
PureBasic Requires Chilkat v11.0.0+

SharePoint Get Files in Root Folder

See more SharePoint Examples

Gets the list of files that exist in the root folder.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkHttp.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkJsonObject.pb"

Procedure ChilkatExample()

    success.i = 0

    ;  This requires the Chilkat API to have been previously unlocked.
    ;  See Global Unlock Sample for sample code.

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

    ;  --------------------------------------------------------------------------------------------------------
    ;  Provide the information needed for Chilkat to automatically fetch the OAuth2.0 access token as needed.

    ;  To create your App Registration for SharePoint in Azure Entra ID,
    ;  see How to Create SharePoint App Registration for OAuth 2.0 Client Credentials

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

    CkJsonObject::ckUpdateString(jsonOAuthCC,"client_id","CLIENT_ID")
    CkJsonObject::ckUpdateString(jsonOAuthCC,"client_secret","SECRET_VALUE")
    CkJsonObject::ckUpdateString(jsonOAuthCC,"scope","https://graph.microsoft.com/.default")
    CkJsonObject::ckUpdateString(jsonOAuthCC,"token_endpoint","https://login.microsoftonline.com/TENANT_ID/oauth2/v2.0/token")

    CkHttp::setCkAuthToken(http, CkJsonObject::ckEmit(jsonOAuthCC))
    ;  --------------------------------------------------------------------------------------------------------

    ;  Indicate that we want a JSON reply
    CkHttp::setCkAccept(http, "application/json;odata=verbose")

    CkHttp::ckSetUrlVar(http,"sharepoint_hostname","example.sharepoint.com")
    sbJson.i = CkStringBuilder::ckCreate()
    If sbJson.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkHttp::ckQuickGetSb(http,"https://graph.microsoft.com/v1.0/sites/SITE_ID/drive/root/children",sbJson)
    If success = 0
        Debug CkHttp::ckLastErrorText(http)
        CkHttp::ckDispose(http)
        CkJsonObject::ckDispose(jsonOAuthCC)
        CkStringBuilder::ckDispose(sbJson)
        ProcedureReturn
    EndIf

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

    CkJsonObject::ckLoadSb(json,sbJson)

    ;  Iterate over the results and get each file's name, size, and last-modified date/time.

    numFiles.i = CkJsonObject::ckSizeOfArray(json,"d.results")
    Debug "Number of Files in the SharePoint root folder = " + Str(numFiles)

    i.i = 0
    While i < numFiles
        CkJsonObject::setCkI(json, i)

        filename.s = CkJsonObject::ckStringOf(json,"d.results[i].Name")
        fileRelativeUri.s = CkJsonObject::ckStringOf(json,"d.results[i].ServerRelativeUrl")
        fileSize.i = CkJsonObject::ckIntOf(json,"d.results[i].Length")

        Debug Str(i + 1) + ": " + filename
        Debug "    Relative URI: " + fileRelativeUri
        Debug "    Size in Bytes: " + Str(fileSize)

        i = i + 1
    Wend

    ;  The output of this program when I tested it:


    CkHttp::ckDispose(http)
    CkJsonObject::ckDispose(jsonOAuthCC)
    CkStringBuilder::ckDispose(sbJson)
    CkJsonObject::ckDispose(json)


    ProcedureReturn
EndProcedure