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

Delphi DLL
var
success: Boolean;
http: HCkHttp;
jsonOAuthCC: HCkJsonObject;
sbJson: HCkStringBuilder;
json: HCkJsonObject;
numFiles: Integer;
i: Integer;
filename: PWideChar;
fileRelativeUri: PWideChar;
fileSize: Integer;

begin
success := False;

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

http := CkHttp_Create();

//  --------------------------------------------------------------------------------------------------------
//  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 := CkJsonObject_Create();
CkJsonObject_UpdateString(jsonOAuthCC,'client_id','CLIENT_ID');
CkJsonObject_UpdateString(jsonOAuthCC,'client_secret','SECRET_VALUE');
CkJsonObject_UpdateString(jsonOAuthCC,'scope','https://graph.microsoft.com/.default');
CkJsonObject_UpdateString(jsonOAuthCC,'token_endpoint','https://login.microsoftonline.com/TENANT_ID/oauth2/v2.0/token');

CkHttp_putAuthToken(http,CkJsonObject__emit(jsonOAuthCC));
//  --------------------------------------------------------------------------------------------------------

//  Indicate that we want a JSON reply
CkHttp_putAccept(http,'application/json;odata=verbose');

CkHttp_SetUrlVar(http,'sharepoint_hostname','example.sharepoint.com');
sbJson := CkStringBuilder_Create();
success := CkHttp_QuickGetSb(http,'https://graph.microsoft.com/v1.0/sites/SITE_ID/drive/root/children',sbJson);
if (success = False) then
  begin
    Memo1.Lines.Add(CkHttp__lastErrorText(http));
    Exit;
  end;

json := CkJsonObject_Create();
CkJsonObject_LoadSb(json,sbJson);

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

numFiles := CkJsonObject_SizeOfArray(json,'d.results');
Memo1.Lines.Add('Number of Files in the SharePoint root folder = ' + IntToStr(numFiles));

i := 0;
while i < numFiles do
  begin
    CkJsonObject_putI(json,i);

    filename := CkJsonObject__stringOf(json,'d.results[i].Name');
    fileRelativeUri := CkJsonObject__stringOf(json,'d.results[i].ServerRelativeUrl');
    fileSize := CkJsonObject_IntOf(json,'d.results[i].Length');

    Memo1.Lines.Add(IntToStr(i + 1) + ': ' + filename);
    Memo1.Lines.Add('    Relative URI: ' + fileRelativeUri);
    Memo1.Lines.Add('    Size in Bytes: ' + IntToStr(fileSize));

    i := i + 1;
  end;

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

CkHttp_Dispose(http);
CkJsonObject_Dispose(jsonOAuthCC);
CkStringBuilder_Dispose(sbJson);
CkJsonObject_Dispose(json);