![]() |
Chilkat HOME Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi DLL Go Java JavaScript Node.js Objective-C PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Swift) SharePoint Get Site IDSee more SharePoint ExamplesThis example demonstrates how to use the Chilkat HttpCurl class together with Microsoft Graph and OAuth2 Client Credentials authentication to retrieve the ID of a SharePoint site. The example automatically obtains an access token, sends a request to Microsoft Graph to locate a site by hostname and site name, and extracts the site's ID, description, and hostname from the JSON response. The retrieved site ID can then be used in subsequent SharePoint operations such as accessing document libraries, browsing folders, and managing files.Note: This example requires Chilkat v11.5.0 or greater.
func chilkatTest() { var success: Bool = false // This example retrieves the Microsoft Graph site ID for a SharePoint site. // // The site ID is required for many subsequent Microsoft Graph operations, // such as enumerating document libraries, listing files, downloading content, // or creating folders within a SharePoint site. success = false // -------------------------------------------------------------------------------------------------------- // Before running this example, create an Azure App Registration and grant it // the Microsoft Graph permissions required to access SharePoint. // // The application will authenticate using OAuth2 Client Credentials. // See: // How to Create SharePoint App Registration for OAuth 2.0 Client Credentials // -------------------------------------------------------------------------------------------------------- // Build a JSON authentication configuration. // HttpCurl will use this information to automatically obtain OAuth2 access tokens. let jsonAuth = CkoJsonObject()! // Enable secret lookup. // // Instead of hard-coding sensitive values such as the client ID, // client secret, and token endpoint, secret specification strings // are used. Chilkat automatically retrieves the actual values from // Windows Credential Manager (Windows) or Apple Keychain (macOS). // // See: // Secret Specification Strings jsonAuth.enableSecrets = true success = jsonAuth.update("oauth2.client_id", value: "!!sharepoint|oauth2|client_id") if success == true { success = jsonAuth.update("oauth2.client_secret", value: "!!sharepoint|oauth2|client_secret") } if success == true { success = jsonAuth.update("oauth2.token_endpoint", value: "!!sharepoint|oauth2|token_endpoint") } if success == false { print("\(jsonAuth.lastErrorText!)") return } // Request Microsoft Graph permissions that were granted to the application. jsonAuth.update("oauth2.scope", value: "https://graph.microsoft.com/.default") // --------------------------------------------------------------------------------------------------- let curl = CkoHttpCurl()! // Associate the OAuth2 configuration with HttpCurl. // // When the request is executed, Chilkat automatically obtains an access token // (if needed) and adds the Authorization: Bearer header to the HTTP request. curl.setAuth(jsonAuth) // Define variables whose values are already known. // // These variables are referenced in the curl command using // {{variable_name}} substitution syntax. curl.setVar("sharepoint_hostname", varValue: "example.sharepoint.com") curl.setVar("site_name", varValue: "test") // Define the values we want to extract from the JSON response. // // The first argument is a JSON path within the response. // The second argument is the name of the HttpCurl variable that // will receive the extracted value. curl.addTargetOutput("id", varName: "site_id") curl.addTargetOutput("description", varName: "site_description") curl.addTargetOutput("siteCollection.hostname", varName: "site_hostname") // Microsoft Graph request: // // GET /sites/{hostname}:/sites/{site-name} // // This returns information about the SharePoint site, including // the site ID that is needed for many later SharePoint operations. // // No Authorization header is included because HttpCurl automatically // adds it when OAuth2 authentication is configured. var curlCommand: String? = "curl -X GET \"https://graph.microsoft.com/v1.0/sites/{{sharepoint_hostname}}:/sites/{{site_name}}\"" // Execute the request. success = curl.doYourThing(curlCommand) if success == false { print("\(curl.lastErrorText!)") return } // A successful Graph response should return HTTP 200. // Any other status code typically indicates an authentication, // permission, or resource lookup error. var statusCode: Int = curl.statusCode.intValue if statusCode != 200 { print("\(curl.responseBodyStr!)") print("status code = \(statusCode)") return } // Verify that all target outputs were successfully extracted. // // The special variable name "!" tells VarDefined to return true // only if every target output defined by AddTargetOutput was found // in the response JSON. if curl.varDefined("!") == false { print("\(curl.responseBodyStr!)") print("Not all target outputs were found...") return } // Display the extracted values. print("site_id = \(curl.getVar("site_id")!)") print("site_description = \(curl.getVar("site_description")!)") print("site_hostname = \(curl.getVar("site_hostname")!)") print("Success.") } |
||||
© 2000-2026 Chilkat Software, Inc. All Rights Reserved.