PowerShell SharePoint Online: The remote server returned an error: (403) Forbidden

This PowerShell SharePoint tutorial, we will discuss how to solve The remote server returned an error: (403) Forbidden.

Recently, I was taking a SharePoint corporate training, then we got the issue while working with SharePoint Online site using PowerShell. The error says:

Exception calling “ExecuteQuery” with “0” argument(s): The remote server returned an error: (403) Forbidden.

The remote server returned an error: (403) Forbidden

The remote server returned an error: (403) Forbidden PowerShell SharePoint Online

Here, we were trying to retrieve the web title using PowerShell with CSOM (using client dlls to connect to SharePoint Online sites).

Below code, we were writing to retrieve web title using PowerShell SharePoint Online.

Add-Type -Path "c:\Bijay\Microsoft.SharePoint.Client.dll"  
Add-Type -Path "c:\Bijay\Microsoft.SharePoint.Client.Runtime.dll"   

$siteUrl = "https://<tenantname>.sharepoint.com/sites/TSInfoIntranet" 
$username = "bijay@<tenantname>.onmicrosoft.com" 
$password = Read-Host -Prompt "Enter password" -AsSecureString  

$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)  
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $password)  
$ctx.Credentials = $credentials 

$rootWeb = $ctx.Web  
$ctx.Load($rootWeb) 
$ctx.ExecuteQuery()

Write-Host $rootWeb.Title  

But when I run the PowerShell script using Windows PowerShell ISE, it gave an error as Exception calling “ExecuteQuery” with “0” argument(s): The remote server returned an error: (403) Forbidden.

From the error, it looks like there was an error in the login details. I verified the Site URL, login name and password and everything was perfect.

Also, copy the SharePoint client dlls from one more system where it was working fine, but still the same error.

The solution to solve the error is to install SharePoint Online client component SDK.

Download SharePoint Online client component SDK and install it. Then restart the PowerShell and then you can refer it from the below URL directly.

Add-Type -Path "E:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"  
Add-Type -Path "E:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

Here I have installed in E drive in my local system.

After you install the SharePoint Online SDK, it will create a structure like below and it will also contain the required dlls.

E:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\

After this the error “Exception calling “ExecuteQuery” with “0” argument(s): The remote server returned an error: (403) Forbidden.” will not come.

The other way, we can also install it from NuGet packages.

You may like following SharePoint PowerShell tutorials:

This PowerShell SharePoint tutorial, we learned how to solve the error: Exception calling “ExecuteQuery” with “0” argument(s): The remote server returned an error: (403) Forbidden.

>