2

Is it possible to use the context of the person whoever is running the PowerShell and get the list items from SharePoint online?

I am trying not to have the Get-Credential login prompt or having the User name password in some file or in the PowerShell file/Windows Credentials Manager

1 Answer 1

3

You can use the below PowerShell code:

$ctx=New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)

$Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials 

$ctx.Credentials = $Credentials; 

$web = $ctx.Web
$ctx.Load($web);
$ctx.ExecuteQuery();

Note :

  • The logged in user must have appropriate permissions to SharePoint to do the task.

  • The account should not have MFA enabled.

Reference URL:

Instantiate SharePoint Client Context using current user credentials in PowerShell

How to fix the "403 error, it says Exception calling ExecuteQuery with 0 arguments" from PowerShell to SharePoint Online connection?

In order to fix the 403 error, we need need to follow the below steps:

  1. Open PowerShell and type “notepad $profile”
  2. Ensure the 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.

Example:

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

#SOM
Add-PSSnapIn Microsoft.SharePoint.PowerShell

Reference:

FIXED – 403 ExecuteQuery CSOM from a SharePoint Serve

3
  • I tried this and this gives me 403 error, it says Exception calling ExecuteQuery with 0 arguments Jun 18, 2020 at 20:54
  • just a note, we login using Domain\UserName and SP Online has permissions given to [email protected] Jun 18, 2020 at 20:55
  • Check the updated answer - it might help you to fix the 403 error.
    – SP 2022
    Jun 19, 2020 at 8:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.