Sample code for 30+ languages & platforms
PowerBuilder 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 PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Http
oleobject loo_JsonOAuthCC
oleobject loo_SbJson
oleobject loo_Json
integer li_NumFiles
integer i
string ls_Filename
string ls_FileRelativeUri
integer li_FileSize

li_Success = 0

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

loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")
if li_rc < 0 then
    destroy loo_Http
    MessageBox("Error","Connecting to COM object failed")
    return
end if

//  --------------------------------------------------------------------------------------------------------
//  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

loo_JsonOAuthCC = create oleobject
li_rc = loo_JsonOAuthCC.ConnectToNewObject("Chilkat.JsonObject")

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

loo_Http.AuthToken = loo_JsonOAuthCC.Emit()
//  --------------------------------------------------------------------------------------------------------

//  Indicate that we want a JSON reply
loo_Http.Accept = "application/json;odata=verbose"

loo_Http.SetUrlVar("sharepoint_hostname","example.sharepoint.com")
loo_SbJson = create oleobject
li_rc = loo_SbJson.ConnectToNewObject("Chilkat.StringBuilder")

li_Success = loo_Http.QuickGetSb("https://graph.microsoft.com/v1.0/sites/SITE_ID/drive/root/children",loo_SbJson)
if li_Success = 0 then
    Write-Debug loo_Http.LastErrorText
    destroy loo_Http
    destroy loo_JsonOAuthCC
    destroy loo_SbJson
    return
end if

loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")

loo_Json.LoadSb(loo_SbJson)

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

li_NumFiles = loo_Json.SizeOfArray("d.results")
Write-Debug "Number of Files in the SharePoint root folder = " + string(li_NumFiles)

i = 0
do while i < li_NumFiles
    loo_Json.I = i

    ls_Filename = loo_Json.StringOf("d.results[i].Name")
    ls_FileRelativeUri = loo_Json.StringOf("d.results[i].ServerRelativeUrl")
    li_FileSize = loo_Json.IntOf("d.results[i].Length")

    Write-Debug string(i + 1) + ": " + ls_Filename
    Write-Debug "    Relative URI: " + ls_FileRelativeUri
    Write-Debug "    Size in Bytes: " + string(li_FileSize)

    i = i + 1
loop

//  The output of this program when I tested it:


destroy loo_Http
destroy loo_JsonOAuthCC
destroy loo_SbJson
destroy loo_Json