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

Unicode C
#include <C_CkHttpW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkStringBuilderW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkHttpW http;
    HCkJsonObjectW jsonOAuthCC;
    HCkStringBuilderW sbJson;
    HCkJsonObjectW json;
    int numFiles;
    int i;
    const wchar_t *filename;
    const wchar_t *fileRelativeUri;
    int fileSize;

    success = FALSE;

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

    http = CkHttpW_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 = CkJsonObjectW_Create();
    CkJsonObjectW_UpdateString(jsonOAuthCC,L"client_id",L"CLIENT_ID");
    CkJsonObjectW_UpdateString(jsonOAuthCC,L"client_secret",L"SECRET_VALUE");
    CkJsonObjectW_UpdateString(jsonOAuthCC,L"scope",L"https://graph.microsoft.com/.default");
    CkJsonObjectW_UpdateString(jsonOAuthCC,L"token_endpoint",L"https://login.microsoftonline.com/TENANT_ID/oauth2/v2.0/token");

    CkHttpW_putAuthToken(http,CkJsonObjectW_emit(jsonOAuthCC));
    //  --------------------------------------------------------------------------------------------------------

    //  Indicate that we want a JSON reply
    CkHttpW_putAccept(http,L"application/json;odata=verbose");

    CkHttpW_SetUrlVar(http,L"sharepoint_hostname",L"example.sharepoint.com");
    sbJson = CkStringBuilderW_Create();
    success = CkHttpW_QuickGetSb(http,L"https://graph.microsoft.com/v1.0/sites/SITE_ID/drive/root/children",sbJson);
    if (success == FALSE) {
        wprintf(L"%s\n",CkHttpW_lastErrorText(http));
        CkHttpW_Dispose(http);
        CkJsonObjectW_Dispose(jsonOAuthCC);
        CkStringBuilderW_Dispose(sbJson);
        return;
    }

    json = CkJsonObjectW_Create();
    CkJsonObjectW_LoadSb(json,sbJson);

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

    numFiles = CkJsonObjectW_SizeOfArray(json,L"d.results");
    wprintf(L"Number of Files in the SharePoint root folder = %d\n",numFiles);

    i = 0;
    while (i < numFiles) {
        CkJsonObjectW_putI(json,i);

        filename = CkJsonObjectW_stringOf(json,L"d.results[i].Name");
        fileRelativeUri = CkJsonObjectW_stringOf(json,L"d.results[i].ServerRelativeUrl");
        fileSize = CkJsonObjectW_IntOf(json,L"d.results[i].Length");

        wprintf(L"%d: %s\n",i + 1,filename);
        wprintf(L"    Relative URI: %s\n",fileRelativeUri);
        wprintf(L"    Size in Bytes: %d\n",fileSize);

        i = i + 1;
    }

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


    CkHttpW_Dispose(http);
    CkJsonObjectW_Dispose(jsonOAuthCC);
    CkStringBuilderW_Dispose(sbJson);
    CkJsonObjectW_Dispose(json);

    }