I have created a c# winform which runs from my desktop, and I am hoping to get it to upload documents to our sharepoint server.
Currently I have the following code:
public void DocUploadTest()
{
SP.ClientContext ctx = new SP.ClientContext("https://sharepoint.oshirowanen.com/sites/oshirodev/");
SP.Web currentWeb = ctx.Web;
ctx.Load(currentWeb);
ctx.ExecuteQuery();
using (FileStream fs = new FileStream(@"c:\sourcefolder\doc1.txt", FileMode.Open))
{
Microsoft.SharePoint.Client.File.SaveBinaryDirect(ctx, "/destinationfolder/doc1.txt", fs, true);
}
}
This code gives the following error message:
The remote server returned an error: (401) Unauthorized.
So I attempted to get authorized by adding the following line:
ctx.Credentials = new NetworkCredential("[user name goes here]", "[password goes here]", "oshirowanen.com");
Where [user name goes here] and [password goes here] are replaced with actual administrator level login credentials which has full access to sharepoint if logging into sharepoint formally from a web browser. This login allows me to create folders, and upload files manually.
Now the code currently looks like this:
public void DocUploadTest()
{
SP.ClientContext ctx = new SP.ClientContext("https://sharepoint.oshirowanen.com/sites/oshirodev/");
ctx.Credentials = new NetworkCredential("[user name goes here]", "[password goes here]", "oshirowanen.com");
SP.Web currentWeb = ctx.Web;
ctx.Load(currentWeb);
ctx.ExecuteQuery();
using (FileStream fs = new FileStream(@"c:\sourcefolder\doc1.txt", FileMode.Open))
{
Microsoft.SharePoint.Client.File.SaveBinaryDirect(ctx, "/destinationfolder/doc1.txt", fs, true);
}
}
I now get the following error message for which I can't seem to find the solution for:
The remote server returned an error: (409) Conflict.
Note 1: The file doc1.txt does not already exist on https://sharepoint.oshirowanen.com/sites/oshirodev/destinationfolder/doc1.txt
Note 2: Both the winform and the sourcefile are on the same local machine.
Note 3: I have also attempted to use a different user/pass which has less permissions, which also gives the 401 error message above, instead of the 409 error message when a administrator login is used.