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

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/2013.

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

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.

You may like 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.

>