9

Hi I have the following piece of code to upload a file to Sharepoint. It uses HTTP PUT:

public static string UploadFile(string destUrl, string sourcePath)
        {
            try
            {
                Uri destUri = new Uri(destUrl);
                FileStream inStream = File.OpenRead(sourcePath);
                WebRequest req = WebRequest.Create(destUri);
                req.Method = "PUT";
                req.Headers.Add("Overwrite", "F");
                req.Timeout = System.Threading.Timeout.Infinite;
                req.Credentials = CredentialCache.DefaultCredentials;
                Stream outStream = req.GetRequestStream();
                string status = CopyStream(inStream, outStream);
                if (status == "success")
                {
                    outStream.Close();
                    WebResponse ores = req.GetResponse();
                    return "success";
                }
                else
                {
                    return status;
                }
            }
            catch (WebException we)
            {
            return we.Message;
            }
            catch (System.Exception ee)
            {
            return ee.Message;
            }
        }

When I run this code I get the exception:

"The remote server returned an error: (409) Conflict."

Does anyone have any ideas as to where I am going wrong?

Thanks,

Alex

4
  • I've just faced the very same issue - check that the URLs are valid!
    – Rashack
    Commented Sep 29, 2009 at 12:54
  • C# noob asks: Where does CopyStream come from? Commented Sep 27, 2011 at 17:56
  • 2
    It's an offshoot of the CopyRiver
    – Amicable
    Commented Jul 3, 2012 at 14:53
  • Where does the CopyStream method come from? Commented Oct 4, 2013 at 14:24

6 Answers 6

8

I've had this issue when I was referencing the url of the document library and not the destination file itself.

i.e. try http://server name/document library name/new file name.doc

1
  • I had the same issue, and your solution works perfectly. Simple and efficient.
    – Antoine
    Commented Jul 1, 2010 at 9:45
1

No clue. But why dont you use Remote Procedure Calls (RPC) thats how i usually do it.

I found this example that might get you started http://geek.hubkey.com/2007/11/upload-file-to-sharepoint-document.html

1

Try:

void StorePlainFile(string target_url, string filename, byte[] file_bytes)
{
      string url = target_url + "/" + filename;
      System.Net.WebClient client = new System.Net.WebClient();
      client.Credentials = System.Net.CredentialCache.DefaultCredentials;
      client.Headers.Add("Overwrite", "F");
      byte[] response = client.UploadData(url, "PUT", file_bytes);
}
1

I haven't solved my problem yet, that's why I'm here, but I know why you're getting this error.

The error results because you are not setting a hidden, but required, field. In my case, I had no columns, and certainly none that were required. However, there is a versioning field that is in conflict.

My intent is to 1) upload the document, and 2) set the document's metadata. 1) and 2) occur over separate HTTP calls. Ideally, I want to do this in a single call, but I don't know how to do this.

To accomplish this, 1) succeeds, so the document appears in the library. Then when I try to update the metadata, that's when I get the 409 error.

I'm pretty sure that I first need to insert a step in between 1) and 2) which first downloads the document's list (or manifest) which would in theory contain the needed versioning information. All I would need to do is set the metadata fields I need, and send back to the server.

No, we don't want to use the Sharepoint API because there are no libraries for it in Java. ;-)

0

Is there a paticular reason you can't just use the Sharepoint API (eg. SPFolder.Files.Add) to upload the file? As follows:

http://msdn.microsoft.com/en-us/library/ms454491.aspx

public void UploadFile(string srcUrl, string destUrl)
{
    if (! File.Exists(srcUrl))
    {
        throw new ArgumentException(String.Format("{0} does not exist", 
            srcUrl), "srcUrl");
    }

    SPWeb site = new SPSite(destUrl).OpenWeb();

    FileStream fStream = File.OpenRead(srcUrl);
    byte[] contents = new byte[fStream.Length];
    fStream.Read(contents, 0, (int)fStream.Length);
    fStream.Close(); 

    EnsureParentFolder(site, destUrl);
    site.Files.Add(destUrl, contents);
}
1
  • 5
    Doesn't this approach require running the code on the server? I think the question is about posting over http.
    – msulis
    Commented Apr 13, 2010 at 0:05
0

Alex, This happened to me too. You probable should create another another lit or document library and upload files into it to test.

You may want to check the variable "destUri" to see if it points to exactly the expected sharepoint list.

My situation is I firstly created a document library "Requrements", there is a typo mistake, then i changed the title to "Requirements". You should notice that sharepoint still keeps the URL to this list as http://server:port/Requrements

This is an exeption. Hopefully it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.