0
votes

I am trying to connect to my Sharepoint Online Site Collection with the help of Visual Code and I am getting below error -The remote server returned an error: (400) Bad Request. Code-

Add-Type -Path "C:\dlls\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\dlls\Microsoft.SharePoint.Client.Runtime.dll"

$userName = "***"
$password = ConvertTo-SecureString "" -AsPlainText -Force

$webURL="https://test.sharepoint.com/sites/test/"
$Context=New-Object Microsoft.SharePoint.Client.ClientContext($webURL)
$Context.Credentials=New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username,$password)
$Web=$context.Web
$Context.Load($web)
$Context.executeQuery()
Write-host $Web.URL
1
your code is fine, i am able to run this in Windows powershell and VS Code with powershell extension installed. However, you may try downloading SharePoint online client components and reference the installed dlls 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" Muhammad Murad Haider
I have installed the latest dlls but still same issue :(. I have enabled powershell ISE mode as well.SB_LoneWolf
have you tested this in powershell?Muhammad Murad Haider
Yes and it is working on Powershell ISE.SB_LoneWolf
try uninstalling and installing powershell extension in VS code againMuhammad Murad Haider

1 Answers

0
votes

If installed the latest SharePoint Online, please check in there is dlls in this folder:

enter image description here

And then in Visual Studio install PowerShell Plugin:

enter image description here

Then it should able to run as expected, this is the result in my side: enter image description here