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

C
#include <C_CkHttp.h>
#include <C_CkJsonObject.h>
#include <C_CkStringBuilder.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkHttp http;
    HCkJsonObject jsonOAuthCC;
    HCkStringBuilder sbJson;
    HCkJsonObject json;
    int numFiles;
    int i;
    const char *filename;
    const char *fileRelativeUri;
    int fileSize;

    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) {
        printf("%s\n",CkHttp_lastErrorText(http));
        CkHttp_Dispose(http);
        CkJsonObject_Dispose(jsonOAuthCC);
        CkStringBuilder_Dispose(sbJson);
        return;
    }

    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");
    printf("Number of Files in the SharePoint root folder = %d\n",numFiles);

    i = 0;
    while (i < numFiles) {
        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");

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

        i = i + 1;
    }

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


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

    }