Skip to content

Instantly share code, notes, and snippets.

@glapointe
Created March 31, 2015 20:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save glapointe/98ca20586fc172f70f4b to your computer and use it in GitHub Desktop.
Save glapointe/98ca20586fc172f70f4b to your computer and use it in GitHub Desktop.
Utility function for connecting to a SharePoint Online Site Collection using the SharePoint CSOM API and PowerShell.
function global:Connect-SPOSite {
[CmdletBinding()]
param (
[Parameter(Mandatory=$true, ValueFromPipeline=$true, Position=0)]
$Url
)
begin {
[System.Reflection.Assembly]::LoadFile("C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.dll") | Out-Null
[System.Reflection.Assembly]::LoadFile("C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.Runtime.dll") | Out-Null
}
process {
if ($global:spoCred -eq $null) {
$cred = Get-Credential -Message "Enter your credentials for SharePoint Online:"
$global:spoCred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($cred.UserName, $cred.Password)
}
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext $Url
$ctx.Credentials = $spoCred
if (!$ctx.ServerObjectIsNull.Value) {
Write-Host "Connected to site: '$Url'" -ForegroundColor Green
}
return $ctx
}
end {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment