Objective-C Requires Chilkat v11.0.0+
Objective-C
SharePoint Get Files in Root Folder
See more SharePoint Examples
Gets the list of files that exist in the root folder.Chilkat Objective-C Downloads
#import <CkoHttp.h>
#import <CkoJsonObject.h>
#import <CkoStringBuilder.h>
#import <NSString.h>
BOOL success = NO;
// This requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
CkoHttp *http = [[CkoHttp alloc] init];
// --------------------------------------------------------------------------------------------------------
// 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
CkoJsonObject *jsonOAuthCC = [[CkoJsonObject alloc] init];
[jsonOAuthCC UpdateString: @"client_id" value: @"CLIENT_ID"];
[jsonOAuthCC UpdateString: @"client_secret" value: @"SECRET_VALUE"];
[jsonOAuthCC UpdateString: @"scope" value: @"https://graph.microsoft.com/.default"];
[jsonOAuthCC UpdateString: @"token_endpoint" value: @"https://login.microsoftonline.com/TENANT_ID/oauth2/v2.0/token"];
http.AuthToken = [jsonOAuthCC Emit];
// --------------------------------------------------------------------------------------------------------
// Indicate that we want a JSON reply
http.Accept = @"application/json;odata=verbose";
[http SetUrlVar: @"sharepoint_hostname" value: @"example.sharepoint.com"];
CkoStringBuilder *sbJson = [[CkoStringBuilder alloc] init];
success = [http QuickGetSb: @"https://graph.microsoft.com/v1.0/sites/SITE_ID/drive/root/children" sbContent: sbJson];
if (success == NO) {
NSLog(@"%@",http.LastErrorText);
return;
}
CkoJsonObject *json = [[CkoJsonObject alloc] init];
[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"] intValue];
NSLog(@"%@%d",@"Number of Files in the SharePoint root folder = ",numFiles);
int i = 0;
while (i < numFiles) {
json.I = [NSNumber numberWithInt: i];
NSString *filename = [json StringOf: @"d.results[i].Name"];
NSString *fileRelativeUri = [json StringOf: @"d.results[i].ServerRelativeUrl"];
int fileSize = [[json IntOf: @"d.results[i].Length"] intValue];
NSLog(@"%d%@%@",i + 1,@": ",filename);
NSLog(@"%@%@",@" Relative URI: ",fileRelativeUri);
NSLog(@"%@%d",@" Size in Bytes: ",fileSize);
i = i + 1;
}
// The output of this program when I tested it: