Android™ Requires Chilkat v11.0.0+
Android™
SharePoint Get Files in Root Folder
See more SharePoint Examples
Gets the list of files that exist in the root folder.Chilkat Android™ Downloads
// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;
import android.app.Activity;
import com.chilkatsoft.*;
import android.widget.TextView;
import android.os.Bundle;
public class SimpleActivity extends Activity {
private static final String TAG = "Chilkat";
// Called when the activity is first created.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
boolean success = false;
// This requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
CkHttp http = new 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
CkJsonObject jsonOAuthCC = new CkJsonObject();
jsonOAuthCC.UpdateString("client_id","CLIENT_ID");
jsonOAuthCC.UpdateString("client_secret","SECRET_VALUE");
jsonOAuthCC.UpdateString("scope","https://graph.microsoft.com/.default");
jsonOAuthCC.UpdateString("token_endpoint","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("application/json;odata=verbose");
http.SetUrlVar("sharepoint_hostname","example.sharepoint.com");
CkStringBuilder sbJson = new CkStringBuilder();
success = http.QuickGetSb("https://graph.microsoft.com/v1.0/sites/SITE_ID/drive/root/children",sbJson);
if (success == false) {
Log.i(TAG, http.lastErrorText());
return;
}
CkJsonObject json = new CkJsonObject();
json.LoadSb(sbJson);
// Iterate over the results and get each file's name, size, and last-modified date/time.
int numFiles = json.SizeOfArray("d.results");
Log.i(TAG, "Number of Files in the SharePoint root folder = " + String.valueOf(numFiles));
int i = 0;
while (i < numFiles) {
json.put_I(i);
String filename = json.stringOf("d.results[i].Name");
String fileRelativeUri = json.stringOf("d.results[i].ServerRelativeUrl");
int fileSize = json.IntOf("d.results[i].Length");
Log.i(TAG, String.valueOf(i + 1) + ": " + filename);
Log.i(TAG, " Relative URI: " + fileRelativeUri);
Log.i(TAG, " Size in Bytes: " + String.valueOf(fileSize));
i = i + 1;
}
// The output of this program when I tested it:
}
static {
System.loadLibrary("chilkat");
// Note: If the incorrect library name is passed to System.loadLibrary,
// then you will see the following error message at application startup:
//"The application <your-application-name> has stopped unexpectedly. Please try again."
}
}