Swift Requires Chilkat v11.0.0+
Swift
SharePoint Get Files in Root Folder
See more SharePoint Examples
Gets the list of files that exist in the root folder.Chilkat Swift Downloads
func chilkatTest() {
var success: Bool = false
// This requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
let http = CkoHttp()!
// --------------------------------------------------------------------------------------------------------
// 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
let jsonOAuthCC = CkoJsonObject()!
jsonOAuthCC.updateString(jsonPath: "client_id", value: "CLIENT_ID")
jsonOAuthCC.updateString(jsonPath: "client_secret", value: "SECRET_VALUE")
jsonOAuthCC.updateString(jsonPath: "scope", value: "https://graph.microsoft.com/.default")
jsonOAuthCC.updateString(jsonPath: "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(name: "sharepoint_hostname", value: "example.sharepoint.com")
let sbJson = CkoStringBuilder()!
success = http.quickGetSb(url: "https://graph.microsoft.com/v1.0/sites/SITE_ID/drive/root/children", sbContent: sbJson)
if success == false {
print("\(http.lastErrorText!)")
return
}
let json = CkoJsonObject()!
json.loadSb(sb: sbJson)
// Iterate over the results and get each file's name, size, and last-modified date/time.
var numFiles: Int = json.size(ofArray: "d.results").intValue
print("Number of Files in the SharePoint root folder = \(numFiles)")
var i: Int = 0
while i < numFiles {
json.i = i
var filename: String? = json.string(of: "d.results[i].Name")
var fileRelativeUri: String? = json.string(of: "d.results[i].ServerRelativeUrl")
var fileSize: Int = json.int(of: "d.results[i].Length").intValue
print("\(i + 1): \(filename!)")
print(" Relative URI: \(fileRelativeUri!)")
print(" Size in Bytes: \(fileSize)")
i = i + 1
}
// The output of this program when I tested it:
}