Reasons and Solutions for 409 Conflict HTTP Error When Uploading Files to SharePoint Using .NET WebRequest

Nov 30, 2025 · Programming · 26 views · 7.8

Keywords: .NET | SharePoint | HTTP WebRequest

Abstract: This article provides an in-depth analysis of the root causes behind the 409 HTTP Conflict error encountered when uploading files to SharePoint using .NET WebRequest. Drawing from real-world cases and official documentation, it explores key factors such as incorrect file path references, version control conflicts, permission issues, and improper metadata handling, offering detailed code examples and solutions to help developers effectively diagnose and resolve these problems.

Problem Background and Error Phenomenon

When using the .NET WebRequest class to upload files to a SharePoint 2010 list or folder, developers may encounter a 409 Conflict HTTP error. This error often occurs randomly during multiple file upload operations, even when the Overwrite header is set to T (overwrite mode) and the target file is pre-deleted.

Common Causes of the 409 Conflict Error

According to the HTTP standard, the 409 error indicates that the server cannot complete the request due to a conflict with the current state of the resource. In the SharePoint environment, this typically involves the following aspects:

Diagnosis and Solutions

To effectively resolve the 409 error, a systematic diagnostic approach is recommended:

  1. Verify Request URI: Ensure the WebRequest URI points to the specific file, not the parent directory. For example, use http://servername/documentlibraryname/newfilename.doc instead of http://servername/documentlibraryname/.
  2. Handle Version Control: Check for existing versions before overwriting a file. Query items via the SharePoint REST API: var request = (HttpWebRequest)WebRequest.Create("https://yoursharepointsite.com/_api/web/lists/getbytitle('Documents')/items?$filter=Title eq 'YourFileName'"); Decide the action strategy based on the response.
  3. Review Permission Settings: Confirm that the account performing the upload has "Contribute" or higher permissions on the library. Consult the SharePoint administrator to adjust permissions if necessary.
  4. Complete Metadata: Before uploading, ensure all required fields (e.g., Title, ContentType) are correctly set. Use the WebRequest Headers property to add metadata: request.Headers.Add("Metadata", "value");.

Code Examples and Best Practices

The following example demonstrates how to build a robust upload method incorporating error handling and conflict prevention:

public void UploadFileToSharePoint(string filePath, string sharePointUrl) {
    try {
        // Check if file exists and handle versions
        var checkRequest = WebRequest.Create($"{sharePointUrl}?$select=Id,Version");
        checkRequest.Credentials = CredentialCache.DefaultCredentials;
        using (var response = checkRequest.GetResponse()) {
            // Parse response to decide whether to overwrite
        }

        // Create upload request
        var request = (HttpWebRequest)WebRequest.Create(sharePointUrl);
        request.Method = "PUT";
        request.Headers.Add("Overwrite", "T");
        request.Credentials = CredentialCache.DefaultCredentials;

        using (var stream = request.GetRequestStream()) {
            var fileBytes = File.ReadAllBytes(filePath);
            stream.Write(fileBytes, 0, fileBytes.Length);
        }

        using (var response = (HttpWebResponse)request.GetResponse()) {
            if (response.StatusCode == HttpStatusCode.Created || response.StatusCode == HttpStatusCode.OK) {
                Console.WriteLine("Upload successful");
            }
        }
    } catch (WebException ex) when ((ex.Response as HttpWebResponse)?.StatusCode == HttpStatusCode.Conflict) {
        Console.WriteLine("409 Conflict Error: " + ex.Message);
        // Implement logging or retry logic
    }
}

Implementing these strategies can significantly reduce the occurrence of 409 errors and enhance the reliability of upload operations.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.