How do I resolve this exception thrown while trying to do CRUD operations on CSOM?

William Thompson 80 Reputation points
2024-06-17T22:08:59.36+00:00

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?

CSOM-6

CSOM-7

Here is the details of the System.Net.WebException error:

System.Net.WebException HResult=0x80131509 Message=The remote server returned an error: (403) Forbidden. Source=System StackTrace: 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.

CSOM-8

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 stepping through my code.

Please advise.

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
10,243 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,616 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. P a u l 10,496 Reputation points
    2024-06-19T18:38:16.3533333+00:00

    "403 Forbidden" means "I trust you are who you say you are (i.e. the username & password are valid), but you don't have permission to access this resource".

    It will help to show the query you're executing so we can instruct further.

    0 comments No comments

  2. Bruce (SqlWork.com) 61,101 Reputation points
    2024-06-19T20:28:40.1+00:00

    You need to know the authentication types that your Sharepoint instance supports. classic Sharepoint uses basic or ntlm challenge/response. modern Sharepoint requires oauth.

    if you are using .net classic you can use the SharePointOnlineCredentials() helper.

    https://learn.microsoft.com/en-us/previous-versions/office/sharepoint-csom/jj170265(v=office.15)

    if you are using .net core, then you need to get the token with your own code:

    https://learn.microsoft.com/en-us/sharepoint/dev/sp-add-ins/using-csom-for-dotnet-standard