Share via


CSOM SharePoint Online PowerShell - Value does not fall with in the expected range

CSOM SharePoint Online PowerShell - Value does not fall with in the expected range

Summary

While using CSOM SharePoint Online SDK Components the first error we encountered is Value does not fall with in the expected range.

Reason

The client context accepts only ABSOLUTE URL not relative.

Code with incorrect URL

Import-Module C:\Temp\CSOM\Microsoft.SharePoint.Client.dll
Import-Module C:\Temp\CSOM\Microsoft.SharePoint.Client.Runtime.dll

$Site = "https:\\domain.SharePoint.com"
$Admin = "Admin@domain.onmicrosoft.com"

$password = Read-Host 'Enter Password' -AsSecureString

$context = New-Object Microsoft.SharePoint.Client.ClientContext($Site)
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Admin , $password)

$context.Credentials = $credentials

Error

Solution

Fix this by changing the URL format. The absolute URL format is PROTOCOL:\domain.com. So before passing the site argument to client context object change the URL in $site varaible.

$Site = "https://domain.SharePoint.com"

Fixed Code

Import-Module C:\Temp\CSOM\Microsoft.SharePoint.Client.dll
Import-Module C:\Temp\CSOM\Microsoft.SharePoint.Client.Runtime.dll

$Site = "https://domain.SharePoint.com"
$Admin = "admin@chensoffice365.onmicrosoft.com"

$password = Read-Host 'Enter Password' -AsSecureString

$context = New-Object Microsoft.SharePoint.Client.ClientContext($Site)
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Admin , $password)

$context.Credentials = $credentials