I am trying to upload a file into a SharePoint library by using WebClient and the WebDav .NET library from independentsoft. First I had the problem that the Upload-method returned an exception with HTTP 403. Than I realized that I have to go through a proxy. Now it's returning me HTTP 405. I also used the ClientOM but wasn't able to configure the proxy settings there so I still get a HTTP 403 there.
The code i am using for WebClient and WebDav .NET is the following:
WebClient:
webClient.UseDefaultCredentials = true;
string[] bypassList = { "localhost" };
webClient.Proxy = new WebProxy("proxy", true, bypassList, new NetworkCredential("user", "password"));
try
{
byte[] response = webClient.UploadFile("http://sharepoint/testBlankSite/testLibrary/TestUpload.txt", "PUT", @"...\Desktop\TestUpload.txt");
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
WebDav .NET
NetworkCredential credential = new NetworkCredential("user", "password");
string[] bypassList = { "localhost" };
WebProxy proxy = new WebProxy("proxy", true, bypassList, new NetworkCredential("user", "password"));
WebdavSession session = new WebdavSession(credential, proxy);
Resource resource = new Resource(session);
try
{
resource.Upload("http://sharepoint/testBlankSite/testLibrary/TestUpload.txt", @"...\Desktop\TestUpload.txt");
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
Has anyone an idea why this isn't working and how I can solve this?
This is a pretty common problem with SharePoint. Check out these links:
SharePoint, WebDAV, and a Case of the 405 Status Codes
SharePoint 2013 - Disable WebDAV use on SharePoint
As @GavinB and @Jeremy mentioned, the alternative is the client-side object model, i.e. Microsoft.SharePoint.Client. This library is just a wrapper and it performs HTTP requests that are similar to the ones you are trying.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With