The remote server returned an error (403) forbidden SharePoint client object model

In this SharePoint Online tutorial, we will discuss how to solve the remote server returned an error (403) forbidden SharePoint client object model error which comes in SharePoint Online/2016/2019/2013.

We will also see how to fix the error, The remote server returned an error: (500) Internal Server Error.

the remote server returned an error (403) forbidden SharePoint client object model

Recently, I was working with the SharePoint client object model (CSOM) (Microsoft.SharePoint.Client.dll) to retrieve the site title. But when I run the code, it gave an error as below while calling the ExecuteQuery() method.

An unhandled exception of type ‘System.Net.WebException’ occurred in Microsoft.SharePoint.Client.dll
Additional information: The remote server returned an error: (403) Forbidden.
Error comes like System.Net.WebException: The remote server returned an error: (403) Forbidden.
at System.Net.HttpWebRequest.GetResponse()
at Microsoft.SharePoint.Client.SPWebRequestExecutor.Execute()
at Microsoft.SharePoint.Client.ClientContext.GetFormDigestInfoPrivate()
at Microsoft.SharePoint.Client.ClientContext.EnsureFormDigest()
at Microsoft.SharePoint.Client.ClientContext.ExecuteQuery()

the remote server returned an error (403) forbidden sharepoint client object model
the remote server returned an error (403) forbidden SharePoint client object model

To work with SharePoint online client object model, we need to use Microsoft.SharePoint.Client.dll and Microsoft.SharePoint.Client.Runtime.dll. We need to authenticate when we are trying to connect to SharePoint online site.

We can do this by using SharePointOnlineCredentials class which will take two parameters username and password in the Credentials property. The password in SharePoint online will be in a SecureString format.

So below is a full piece of CSOM code to retreive SharePoint site title.

private void GetSiteDetailsSPOnline(object sender, EventArgs e)
{
using (ClientContext ctx = new ClientContext("https://onlysharepoint2013.sharepoint.com/sites/sptraining/"))
{
ctx.AuthenticationMode = ClientAuthenticationMode.Default;
SecureString passWord = new SecureString();
string pwd = "MyPassword";
foreach (char c in pwd.ToCharArray()) passWord.AppendChar(c);
ctx.Credentials = new SharePointOnlineCredentials("bijay@XXXXXXXX.onmicrosoft.com", passWord);
Web web = ctx.Web;
ctx.Load(web);
ctx.ExecuteQuery();
lblResult.Text = web.Title;
}
}

Basically, we can solve The remote server returned an error: (403) Forbidden when I try to access Sharepoint error by using SharePointOnlineCredentials.

Read similar SharePoint 2013 client object model errors:

the remote server returned an error (403) forbidden SharePoint 2013/2016 client object model

If you are using client object model code to connect to SharePoint 2013 or SharePoint 2016, Then you can use the NetworkCredential class to pass the network credentials inside the object model code.

Below is the full piece code:

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;
}
}

Here we are passing the network credentials in the NetworkCredential class. Now the error should not come.

The remote server returned an error: (403) Forbidden error in SharePoint client object model console application

If you are trying to connect to SharePoint on-premise site using .Net managed client object model code, then you can write like below to pass the username, password and domain.

using (ClientContext context = new ClientContext("https://SiteURL"))
{
context.AuthenticationMode = ClientAuthenticationMode.Default;
context.Credentials = new NetworkCredential("username", "password", "domain");
Web web = context.Web;
context.Load(web);
context.ExecuteQuery();
}

Basically to resolve The remote server returned an error: (403) Forbidden error, you can pass the credentials to the client context like below:

NetworkCredential credentials =new NetworkCredential("username", "password", "domain");
context .Credentials = credentials;
Or you can also write like below:
context.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;

Then The remote server returned an error: (403) Forbidden error will not appear.

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

In 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 an Administrator account. So you can pass like below:

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

This is how to fix the error, the remote server returned an error 500 internal server error which comes while working with the managed object model in SharePoint 2019/2016/2013.

You may like the following SharePoint client object model tutorials:

I hope this SharePoint tutorial helps to resolve the remote server returned an error (403) forbidden SharePoint client object model, the remote server returned an error (403) forbidden SharePoint or clientcontext executequery () the remote server returned an error 403 forbidden error in SharePoint Online/2016/2013.

>