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

This SharePoint CSOM tutorial, we will discuss how to resolve the issue which comes in SharePoint 2013/2016 “the remote server returned an error 500 internal server error. sharepoint client object model”.

We were working in SharePoint 2016 client object in a console application where we received the below error:

An unhandled exception of type ‘System.Net.WebException’ occurred in System.dll

Additional information: The remote server returned an error: (500) Internal Server Error.

The remote server returned an error: (500) Internal Server Error
The remote server returned an error: (500) Internal Server Error

The remote server returned an error: (500) Internal Server Error

We wrote the below code to retrieve SharePoint site details using CSOM code.

private void GetSiteDetails()
{
using (ClientContext context = new ClientContext("http://mypc/sites/MySP2016SiteCollection/"))
{
context.AuthenticationMode = ClientAuthenticationMode.Default;
context.Credentials = new NetworkCredential(@"MYSP\Administrator", "Qwe@12345");
Web web = context.Web;
context.Load(web);
context.ExecuteQuery();
lblResult.Text = web.Title;
}
}

To connect to SharePoint 2016 site from the client-side object model, we need to provide credentials that we have provided in the below line which is perfect. Here http://mypc/sites/MySP2016SiteCollection/ was my SharePoint on-premise 2016 site.

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

But the error was coming because of the below line:

context.AuthenticationMode = ClientAuthenticationMode.Default;

So I have modified the CSOM code like below to resolve the issue.

private void GetSiteDetails()
{
using (ClientContext context = new ClientContext("http://mypc/sites/MySP2016SiteCollection/"))
{
context.Credentials = new NetworkCredential(@"MYSP\Administrator", "Qwe@12345");
Web web = context.Web;
context.Load(web);
context.ExecuteQuery();
lblResult.Text = web.Title;
}
}

Still if you are getting the above error, then try to change the account name, instead of using Administrator account. So you can pass like below:

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

Sometime you will get the below error also:

I hope this will be helpful to resolve the remote server returned an error 500 internal server error which comes while working with the managed object model in SharePoint 2016/2013.

>