Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Power Pages - General Discussions
Unanswered

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

(0) ShareShare
ReportReport
Posted on by 1
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.png

 

 
 
CSOM-7.png

 

 
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.
 
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.
 
There is something in C# called "secure strong".  I tried using the SecureString class by converting the password strint to a character string and then to this class and using the secure string in the call to:
 
                ctx.Credentials = new System.Net.NetworkCredential(txtUsername.Text, convertedPassword);
 
but it still did not work.
 
Microsoft should offer a solution to this issue.  They have an obligation, I feel, when offering a new technology like Microsoft Lists, to provide a means by which developers can write code to interface with these objects.
 
CSOM-8.png

 

 
Categories:
  • ragavanrajan Profile Picture
    7,036 Most Valuable Professional on at
    Re: How do I resolve this exception thrown while trying to do CRUD operations on CSOM?

    Hi @Jetslinger145 

     

    Welcome to the Power Pages forum. This forum is exclusive for solving Power Pages-related queries. It would be best if you found the appropriate forum to ask about your SharePoint and List-related queries. 

     

    However, Looking at your error. 

    My first hunch is it is related to the user permissions or proxy settings. Also, make sure the site URL is accessible by your account. 

     

    Here is a sample to try 

     

    using Microsoft.SharePoint.Client;
    using System;
    using System.Security;
    
    namespace SharePointCSOM
    {
     class Program
     {
     static void Main(string[] args)
     {
     string siteUrl = "https://<your-sharepoint-site>/sites/<your-site>";
     string username = "<your-username>";
     string password = "<your-password>";
    
     // Securely store the password
     SecureString securePassword = new SecureString();
     foreach (char c in password)
     {
     securePassword.AppendChar(c);
     }
    
     // Create a SharePoint client context
     using (ClientContext ctx = new ClientContext(siteUrl))
     {
     ctx.Credentials = new SharePointOnlineCredentials(username, securePassword);
    
     try
     {
     // Load and execute the query
     Web web = ctx.Web;
     ctx.Load(web);
     ctx.ExecuteQuery();
     Console.WriteLine("Connected to SharePoint site: " + web.Title);
     }
     catch (Exception ex)
     {
     Console.WriteLine("Error: " + ex.Message);
     }
     }
     }
     }
    }
    

     

    Hope it helps.
    ------------

    If you like this post, give it a Thumbs up. Where it solved your request, Mark it as a Solution to enable other users to find it.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Michael Gernaey – Community Spotlight

We are honored to recognize Michael Gernaey as our June 2025 Community…

Congratulations to the May Top 10 Community Leaders!

These are the community rock stars!

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

Leaderboard >