DataFlex
DataFlex
SharePoint Get Site ID
See more SharePoint Examples
This 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.Chilkat DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Variant vJsonAuth
Handle hoJsonAuth
Handle hoCurl
String sCurlCommand
Integer iStatusCode
String sTemp1
Boolean bTemp1
Move False To iSuccess
// 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.
Move False To iSuccess
// --------------------------------------------------------------------------------------------------------
// 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.
Get Create (RefClass(cComChilkatJsonObject)) To hoJsonAuth
If (Not(IsComObjectCreated(hoJsonAuth))) Begin
Send CreateComObject of hoJsonAuth
End
// 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
Set ComEnableSecrets Of hoJsonAuth To True
Get ComUpdateString Of hoJsonAuth "oauth2.client_id" "!!sharepoint|oauth2|client_id" To iSuccess
If (iSuccess = True) Begin
Get ComUpdateString Of hoJsonAuth "oauth2.client_secret" "!!sharepoint|oauth2|client_secret" To iSuccess
End
If (iSuccess = True) Begin
Get ComUpdateString Of hoJsonAuth "oauth2.token_endpoint" "!!sharepoint|oauth2|token_endpoint" To iSuccess
End
If (iSuccess = False) Begin
Get ComLastErrorText Of hoJsonAuth To sTemp1
Showln sTemp1
Procedure_Return
End
// Request Microsoft Graph permissions that were granted to the application.
Get ComUpdateString Of hoJsonAuth "oauth2.scope" "https://graph.microsoft.com/.default" To iSuccess
// ---------------------------------------------------------------------------------------------------
Get Create (RefClass(cComChilkatHttpCurl)) To hoCurl
If (Not(IsComObjectCreated(hoCurl))) Begin
Send CreateComObject of hoCurl
End
// 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.
Get pvComObject of hoJsonAuth to vJsonAuth
Get ComSetAuth Of hoCurl vJsonAuth To iSuccess
// Define variables whose values are already known.
//
// These variables are referenced in the curl command using
// {{variable_name}} substitution syntax.
Send ComSetVar To hoCurl "sharepoint_hostname" "example.sharepoint.com"
Send ComSetVar To hoCurl "site_name" "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.
Send ComAddTargetOutput To hoCurl "id" "site_id"
Send ComAddTargetOutput To hoCurl "description" "site_description"
Send ComAddTargetOutput To hoCurl "siteCollection.hostname" "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.
Move 'curl -X GET "https://graph.microsoft.com/v1.0/sites/{{sharepoint_hostname}}:/sites/{{site_name}}"' To sCurlCommand
// Execute the request.
Get ComDoYourThing Of hoCurl sCurlCommand To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoCurl To sTemp1
Showln sTemp1
Procedure_Return
End
// A successful Graph response should return HTTP 200.
// Any other status code typically indicates an authentication,
// permission, or resource lookup error.
Get ComStatusCode Of hoCurl To iStatusCode
If (iStatusCode <> 200) Begin
Get ComResponseBodyStr Of hoCurl To sTemp1
Showln sTemp1
Showln "status code = " iStatusCode
Procedure_Return
End
// 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.
Get ComVarDefined Of hoCurl "!" To bTemp1
If (bTemp1 = False) Begin
Get ComResponseBodyStr Of hoCurl To sTemp1
Showln sTemp1
Showln "Not all target outputs were found..."
Procedure_Return
End
// Display the extracted values.
Get ComGetVar Of hoCurl "site_id" To sTemp1
Showln "site_id = " sTemp1
Get ComGetVar Of hoCurl "site_description" To sTemp1
Showln "site_description = " sTemp1
Get ComGetVar Of hoCurl "site_hostname" To sTemp1
Showln "site_hostname = " sTemp1
Showln "Success."
End_Procedure