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

Lianja
llSuccess = .F.

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

loHttp = createobject("CkHttp")

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

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

loHttp.AuthToken = loJsonOAuthCC.Emit()
//  --------------------------------------------------------------------------------------------------------

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

loHttp.SetUrlVar("sharepoint_hostname","example.sharepoint.com")
loSbJson = createobject("CkStringBuilder")
llSuccess = loHttp.QuickGetSb("https://graph.microsoft.com/v1.0/sites/SITE_ID/drive/root/children",loSbJson)
if (llSuccess = .F.) then
    ? loHttp.LastErrorText
    release loHttp
    release loJsonOAuthCC
    release loSbJson
    return
endif

loJson = createobject("CkJsonObject")
loJson.LoadSb(loSbJson)

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

lnNumFiles = loJson.SizeOfArray("d.results")
? "Number of Files in the SharePoint root folder = " + str(lnNumFiles)

i = 0
do while i < lnNumFiles
    loJson.I = i

    lcFilename = loJson.StringOf("d.results[i].Name")
    lcFileRelativeUri = loJson.StringOf("d.results[i].ServerRelativeUrl")
    lnFileSize = loJson.IntOf("d.results[i].Length")

    ? str(i + 1) + ": " + lcFilename
    ? "    Relative URI: " + lcFileRelativeUri
    ? "    Size in Bytes: " + str(lnFileSize)

    i = i + 1
enddo

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


release loHttp
release loJsonOAuthCC
release loSbJson
release loJson