Skip to content

Instantly share code, notes, and snippets.

@jcallaghan
Last active October 24, 2019 12:41
Show Gist options
  • Save jcallaghan/d61d4bd8d23fbadf9687434a0a49dd2d to your computer and use it in GitHub Desktop.
Save jcallaghan/d61d4bd8d23fbadf9687434a0a49dd2d to your computer and use it in GitHub Desktop.
Connect to SharePoint Online using CSOM DLLs (Legacy method)
$username = "username@tenant.onmicrosoft.com"
$securePassword = Read-Host -Prompt "Enter password" -AsSecureString
$SecurePassword = $securePassword | ConvertTo-SecureString -AsPlainText -Force
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Taxonomy.dll"
function Connect-SPO{
Param(
[string] $url
)
$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)
$clientContext.Credentials = $credentials
if (!$clientContext.ServerObjectIsNull.Value){
Write-Host "Connected to SharePoint Online site: '$Url'"
return $clientContext
}else{
Write-Host "Client context not available." -ForegroundColor Red
break
}
}
$clientContext = Connect-SPO -url "https://tenant.sharepoint.com/"
$web = $clientContext.Web
$clientContext.Load($web)
$clientContext.ExecuteQuery()
Write-Host "Processing web with URL: '$($web.Url)'"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment