0

I am trying to load the on premise SharePoint site using below PowerShell code:

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"
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Publishing.dll"
$siteURL = ""
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)
$site = $ctx.Web
$ctx.Load($site)
$ctx.ExecuteQuery()
$pageName = "Test.aspx"
$file = $ctx.Web.GetFileByServerRelativeUrl("/SitePages/Test.aspx")
$ctx.Load($file)
$ctx.ExecuteQuery()

But I am getting this error message:

Exception calling "ExecuteQuery" with "0" argument(s): "The remote server returned an error: (500) Internal Server
Error."
At line:1 char:1
+ $ctx.ExecuteQuery()
+ ~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : WebException

1 Answer 1

0

Are able to open the site directly from the browser?

This could happen due to many reasons :

  1. Check the memory usages of the server... try to close all unwanted processor sessions. It also could be due over app pool running in the server.

You will get more details about the this error, once we check the below logs :

  1. ULS logs
  2. Event Viewer logs
  3. IIS logs

If you are able open the site from browser but not through the code, then this also could be the reason the way you are trying to authenticate to SharePoint.

For that try to use network credential like below :

context.Credentials = new NetworkCredential(@"MYSP\Bijay", "Qwe@12345");

If this also doesn't work, try to pass the different account.

For details refer to the below article :

server returned an error 500 internal server error. sharepoint client object model

Updated:

For connection SharePoint on-premise, you can use the below code:

#Setup Credentials to connect
$Credentials = [System.Net.CredentialCache]::DefaultCredentials  #Current User Credentials
#connect using user account/password
#$Credentials = New-Object System.Net.NetworkCredential($UserName, (ConvertTo-SecureString $Password -AsPlainText -Force))
#For Office 365, Use:
#$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,(ConvertTo-SecureString $Password -AsPlainText -Force))

#Set up the context
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
$Context.Credentials = $credentials
$web = $Context.Web

Reference

5
  • i have added $creds = New-Object System.Net.NetworkCredential("", "") $ctx.Credentials = $creds
    – Liz
    Jun 1, 2020 at 14:21
  • still same error i am getting
    – Liz
    Jun 1, 2020 at 14:21
  • I am able to connect using the same code - you can try other methods to connect, refer to the updated section of the answer.
    – SP 2022
    Jun 1, 2020 at 14:45
  • I tried all the method same error
    – Liz
    Jun 1, 2020 at 15:52
  • after ExecuteQuery () i am getting error
    – Liz
    Jun 1, 2020 at 15:53

Your Answer

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

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