Click here to Skip to main content
15,938,218 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How do I resolve this exception thrown while trying to do CRUD operations on CSOM?

Let me break this down for you. CRUD, of course, means "Create Read Update and Delete". CSOM is the tech for connection C# to a Sharepoint List (or, I assume, also a Microsoft List) in order to programmatically make additions or mortifications to said list from C# code.

I think that, due to the fact that the Microsoft List resides on a highly secure remote source dripping with security systems, I get an exception thrown when I set up a query and perform an "ExecuteQuery" from the ClientContext class. I can provide some source code if necessary but I do not think it is necessary. The question I have is simply this. What would cause a 403 Forbidden error and how can I find a way to resolve it?[^][^]

Here is the details of the System.Net.WebException error:
  Message=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()
   at Dashboard.frmSettings.btnProcess_Click(Object sender, EventArgs e) in C:\source\Dashboard\Dashboard\frmSettings.cs:line 635

  This exception was originally thrown at this call stack:
    [External Code]
    Dashboard.frmSettings.btnProcess_Click(object, System.EventArgs) in frmSettings.cs

What looks most important to me is:
System.Net.WebException HResult=0x80131509

Before I dig into this issue any further, I would like to take a moment and ask some simplier questions which could lead to answers that might help:
1. In order to make sure I am able to access the Microsoft List from the server, I am using a username and password in setting up the ClientContext class like this:
Using (ClientContext ctx = new ClientContext("(URL TO THE SHAREPOING SITE)"))
            ctx.Credentials = new System.Net.NetworkCredential(txtUsername.Text, txtPassword.Text);

So, let me ask this. Could the issue be as simple as the fact that the password has to be some sort of special string class? I remember seeing something about this one time.
I dismissed it because I thought it was some sort of APP.CONFIG thing used with ASP.NET

Now, if this, that I just mentioned is not the issue, then let's continue. Microsoft should provide a way to programmatically interface with low code or no code solutions and if they pride themselves in providing solutions for developers, then they products they offer for solutions.

Bing is a Microsoft product. Perhaps Bing's AI product "Copilot" can provide some answers.

I asked Bing Copilot:
hOW DO i FIX System.Net.WebException HResult=0x80131509

Copilot said:
The System.Net.WebException with HResult=0x80131509 often occurs when there’s an issue with the underlying connection. Let’s explore some potential solutions:

1. Check the Server:
o Verify that the server you’re connecting to is operational and not experiencing issues. Look at logs or event viewer entries for any clues.
o Ensure that you’re connecting to the correct server (consider proxy settings, etc.).
o Check the HTTP status code returned in the response (if available).

2. Exception Tracking:
o Wrap your code in exception-catching blocks and print out the full stack trace. This can provide more information about the problem.
o Consider enabling network tracing to see what’s happening over the wire.

3. Disable Expect100Continue:
o In some cases, setting System.Net.ServicePointManager.Expect100Continue to false can resolve the issue.
o You can do this with: System.Net.ServicePointManager.Expect100Continue = false;

4. Ensure TLS Support:
o If you’re using PowerShell, enable TLS by adding this line before your Invoke-RestMethod or Invoke-WebRequest call: [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls3.
o For C# applications, ensure you’re using the most recent version of the .NET Framework.

5. Override GetWebRequest Method:
o Add a method to your class that overrides GetWebRequest. This allows you to access the HttpWebRequest object.

So, this leads me to the following questions:

1. How do I "Check the Server"? How would I verify that the server I am connecting to is operational nad not experiencing issues? I am able to log into the dasbboard account (we literally call it "dashboard") and from there I am able to freely look at our Microsoft List and view it in grid or row style. Doesn't that mean that the network connection checks out? What, specifically, do I need to do to further check the network?
2. This leads me to "Consider enabling network tracing to see what’s happening over the wire". How do I do that?
3. I tried adding the line System.Net.ServicePointManager.Expect100Continue = false; prior to the line of code that causes the exception to be thrown and it did not fix the problem
4. it says "For C# applications, ensure you’re using the most recent version of the .NET Framework.". How do I know I am using the latest version of the .NET Framework and where do I go to get the latest version. This might solve the problem.
5. How do I add a method to my class that overrides GetWebRequest?

Doing a search to find how to fix this 403 Forbidden error I found some information on the file and folder permissions and fixing it by setting the permissions to the file and the folder. I don't have the ability to do that, but I do have the ability to go into Microsoft List and get a link for permissions to edit. I tried using this link but I still got the 403 Forbidden error while steppin through my code.

Please advise.

What I have tried:

I tried all the steps I described in the problem
Updated 17-Jun-24 22:21pm

Error 403 includes the error reason: "Forbidden".

All it is saying is "Your logged in user does not have permission to access this item or object". The server is happy with your login and understands your request - but cannot provide you the access you are asking for. You need to talk to the server admins about this: it absolutely needs full access to the server to work out how fix this, and we don't have any of that!
Share this answer
You went into a lot of detail about the wrong path you went down.

If you're using SharePoint Online, you cannot use NetworkCredentials. You have to get an access token and pass that token in each request instead of the NetworkCredentials.

Google for "C# SharePoint Online get access token" for examples that fit your environment.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900