Quantcast
Channel: SharePoint Pals - PowerShell
Viewing all articles
Browse latest Browse all 31

ExecuteQuery” with “0” argument(s): “The remote server returned an error: (403) Forbidden–On SharePoint PowerShell CSOM

$
0
0

The above said exception looks very strange. Because, I have installed the SP Online Management Shell and all the DLLs were properly installed. But, when I try to execute the below code, on the ExecuteQuery, I am getting an exception of 403 forbidden.

 $UserName="sathish@*******.onmicrosoft.com"  $Password = ConvertTo-SecureString  "********" -AsPlainText -Force  $URL="https://*******.sharepoint.com/sites/DeveloperSite"  $SiteListName="SiteRequests"  $SPCredentials = New-Object -typename System.Management.Automation.PSCredential -argumentlist $UserName,$Password  Connect-PnPOnline -Url $URL -Credentials $SPCredentials               $SPOCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $Password)     $context = New-Object Microsoft.SharePoint.Client.ClientContext($URL)     $context.Credentials = $SPOCredentials       $web = $context.web      $context.load($web)     $context.ExecuteQuery()         Disconnect-PnPOnline 

After few investigations found that, there is a conflict between the DLLs in the loading order. The only thing on the above code is, I am using SPO Online commands (SPO) for few reason and the context.executequery (CSOM) as well. That’s were the issue starts.

Then by loading the Microsoft.SharePoint.Client.dll and the Microsoft.SharePoint.Client.Runtime.dll explicitly, the exception gone. The complete code is as follows.

 $UserName="sathish@*******.onmicrosoft.com"  $Password = ConvertTo-SecureString  "********" -AsPlainText -Force  $URL="https://*******.sharepoint.com/sites/DeveloperSite"  $SiteListName="SiteRequests"  $SPCredentials = New-Object -typename System.Management.Automation.PSCredential -argumentlist $UserName,$Password  Connect-PnPOnline -Url $URL -Credentials $SPCredentials          Import-Module "C:\SATHISH\\Archive\ClientDLLs.O365\Microsoft.SharePoint.Client.dll"     Import-Module "C:\SATHISH\\Archive\ClientDLLs.O365\Microsoft.SharePoint.Client.Runtime.dll"       $SPOCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $Password)     $context = New-Object Microsoft.SharePoint.Client.ClientContext($URL)     $context.Credentials = $SPOCredentials       $web = $context.web      $context.load($web)     $context.ExecuteQuery()         Disconnect-PnPOnline 

Hope this will help while implementing both SPO and CSOM on the same PowerShell Script.

Happy Coding,

Sathish Nadarajan.


Viewing all articles
Browse latest Browse all 31

Trending Articles