Unicode C++
Unicode C++
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
#include <CkHttpW.h>
#include <CkJsonObjectW.h>
#include <CkStringBuilderW.h>
void ChilkatSample(void)
{
bool success = false;
// This requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
CkHttpW http;
// --------------------------------------------------------------------------------------------------------
// 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
CkJsonObjectW jsonOAuthCC;
jsonOAuthCC.UpdateString(L"client_id",L"CLIENT_ID");
jsonOAuthCC.UpdateString(L"client_secret",L"SECRET_VALUE");
jsonOAuthCC.UpdateString(L"scope",L"https://graph.microsoft.com/.default");
jsonOAuthCC.UpdateString(L"token_endpoint",L"https://login.microsoftonline.com/TENANT_ID/oauth2/v2.0/token");
http.put_AuthToken(jsonOAuthCC.emit());
// --------------------------------------------------------------------------------------------------------
// Indicate that we want a JSON reply
http.put_Accept(L"application/json;odata=verbose");
http.SetUrlVar(L"sharepoint_hostname",L"example.sharepoint.com");
CkStringBuilderW sbJson;
success = http.QuickGetSb(L"https://graph.microsoft.com/v1.0/sites/SITE_ID/drive/root/children",sbJson);
if (success == false) {
wprintf(L"%s\n",http.lastErrorText());
return;
}
CkJsonObjectW json;
json.LoadSb(sbJson);
// Iterate over the results and get each file's name, size, and last-modified date/time.
int numFiles = json.SizeOfArray(L"d.results");
wprintf(L"Number of Files in the SharePoint root folder = %d\n",numFiles);
int i = 0;
while (i < numFiles) {
json.put_I(i);
const wchar_t *filename = json.stringOf(L"d.results[i].Name");
const wchar_t *fileRelativeUri = json.stringOf(L"d.results[i].ServerRelativeUrl");
int fileSize = json.IntOf(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:
}