views:

30

answers:

1

I have a sharepoint server on which some files are hosted.

I have some .Net code that creates a WebRequest to GET some of the files. This is is working correctly.

When I try to use similar code to PUT a file at the same location, I'm getting a:

System.Net.WebException: The remote server returned an error: (405) Method Not Allowed on my request.GetResponse();

I am not getting any InnerExceptions.

Here is my code:

        var requestPath = Path.Combine(serverBasePath, library.GetLibraryPath(), Path.GetFileName(filePath));

        var request = WebRequest.Create(requestPath);
        request.Credentials = CredentialCache.DefaultNetworkCredentials;
        request.Method = "PUT";
        var buffer = new byte[1024];

        using (var requestStream = request.GetRequestStream())
        using (var fileStream = new FileStream(filePath, FileMode.Open))
            for (int i = fileStream.Read(buffer, 0, buffer.Length); i > 0; i = fileStream.Read(buffer, 0, buffer.Length))
                requestStream.Write(buffer, 0, i);


        using (request.GetResponse()) { }

        return requestPath;

Using the same code (replacing PUT with GET and changing filepath to an existing file) works.

Via the Sharepoint Web interface I can upload new files. I have also tried changing the authorization in the parameters to put Full Control for all Authentified Users on this directory.

+2  A: 

Hi, I usually use Fiddler when I get HTTP errors, but that is an aside.

I located this blog about the 405 error and it would seem that there is a verb mismatch:

http://sharepointinterface.com/2009/12/28/sharepoint-webdav-and-a-case-of-the-405-status-codes/

Can you add the verb PUT to the web site, if that is a wise thing to do of course?

Monkieboy
I read the blog you posted and tried the author's solution, which was to disable WebDAV publishing and it worked.
bleakcabal
I'm the author of the blog post cited; glad to hear that it worked for you. One item worth mentioning: the fix seems fairly specific to SharePoint 2007 running under IIS7.x. I didn't do extensive testing with SharePoint 2010, but initial tests suggest that the same issue isn't present. Just an FYI/heads-up.
Sean McDonough