Last active
October 24, 2019 12:41
-
-
Save jcallaghan/d61d4bd8d23fbadf9687434a0a49dd2d to your computer and use it in GitHub Desktop.
Connect to SharePoint Online using CSOM DLLs (Legacy method)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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