SharePoint, Office 365, Azure, and Front end JS geek. – Chicago IL
SharePoint, Office 365, Azure, and Front end JS geek. – Chicago IL

FIXED – 403 ExecuteQuery CSOM from a SharePoint Server

I came across this error when running CSOM requests to Office 365.   While troubleshooting the CSOM call worked beautifully from “powershell_ise” but not regular “powershell.”  Fiddler monitoring showed the cmdlet making HTTP traffic correctly with login handshake from ISE.    However, the regular “powershell” window did not attempt any login handshake.

Strange?   Definitely.

The root cause was “$profile” loading the Server Object Model (SOM) cmdlets before CSOM DLL were loaded.   Why would that matter?  How could it cause a CSOM issue?   There seems to be a DLL namespace overlap internally between these plugins (SOM, CSOM, SPO) so loading sequence matters.  A lot.

By proactively loading the CSOM DLL in $profile before SOM, everything worked correctly from both ISE and the regular PowerShell console.

Hope this helps! 




  1. Run CSOM request in PowerShell
  2. From a SharePoint Server on premise (2013 here)
  3. Which also has Microsoft SharePoint Online (SPO) cmdlets installed (
  4. See this error
  5. Exception calling “ExecuteQuery” with “0” argument(s):  “The remote server returned an error:  (403) Forbidden.





  1. Open PowerShell and type “notepad $profile”
  2. Ensure below code is present.
  3. NOTE – CSOM must load before SOM (Server Object Model) for requests to execute correctly.   Workaround for an internal Microsoft naming overlap.  Both are probably using the same object somewhere.  Loading CSOM first allows CSOM to reserve the namespace first.


Code [$profile]



Code [csom-only-test.ps1]

  • Shivaprasad Rao

    @SPJeff: This works…g8 analysis.

    • Glad you found it useful!

  • durgai raja

    i am getting error Add-PSSnapIn : No snap-ins have been registered for Windows PowerShell version 3.
    At line:1 char:1
    + Add-PSSnapIn Microsoft.SharePoint.PowerShell
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidArgument: (Microsoft.SharePoint.PowerShell:String) [Add-PSSnapin], PSArgumentException
    + FullyQualifiedErrorId : AddPSSnapInRead,Microsoft.PowerShell.Commands.AddPSSnapinCommand

  • Çağrı Karabudak

    Thanks man, nice catch!

  • YAR

    Hello, I am facing an issue here – I created a ps1 named “profile.ps1”. it contains below code:

    notepad $profile

    #CSOM first
    [System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SharePoint.Client”) | Out-Null
    [System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SharePoint.Client.Runtime”) | Out-Null

    Add-PSSnapIn Microsoft.SharePoint.PowerShell

    When I execute this, a blank notepad file opens and an error “The system cannot find the path specified” error.

    Could you please help me to identify where I am going wrong?


    • Hey YAR,

      Remove the first line. “notepad $profile” is what we type interactively to open SP1 for editing. However, that should not be saved within a profile PS1.

      • YAR

        Thank you for the response. Should I have these two lines within my script where I am connecting to SharePoint Online via ClientContext?

        #CSOM first
        [System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SharePoint.Client”) | Out-Null
        [System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SharePoint.Client.Runtime”) | Out-Null

        Add-PSSnapIn Microsoft.SharePoint.PowerShell

        • YAR

          @spjeff:disqus Thanks ! I got it working!

      • Subbu

        Here’s my code:

        function Connect()

        $username =””
        $password = “”
        $securePassword=New-Object SecureString
        foreach($c in $password.ToCharArray()){
        $clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
        $credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)
        $clientContext.Credentials = $credentials;
        if (!$clientContext.ServerObjectIsNull.Value)
        Write-Host “Connected to SharePoint Online site: ‘$SiteUrl'” -ForegroundColor Green
        $web = $clientContext.Web;


        This is my code, where I am getting “403 forbidden” error @ $clientcontext.Load($web) $clientContext.ExecuteQuery();

© Copyright 2016
@ SPJeff

Return to Top ▲Return to Top ▲