Michal B.
Michal B.

Reputation: 5719

Uploading a file to sharepoint using WebDav protocol and VB.NET

I am trying to upload file to sharepoint. Authorization works fine, but it ends up with status OK instead of CREATED. Eventually the file is not created. I do not understand why it's happening since I used approach that seems to work for others (no complaints). Here's the code that I am using:

Public Sub Create()
    Dim szURL1 = "http://host.domain.com/p/projects/4/Proposal/cze.txt"

    Dim szContent = String.Format("Date/Time: {0} {1}", DateTime.Now.ToShortDateString(), DateTime.Now.ToLongTimeString())

    'Define username and password strings.
    Dim domain = "DOMAIN_NAME"
    Dim szUsername = "USER_NAME"
    Dim szPassword = "PASSWORD"

    Dim httpPutRequest As HttpWebRequest = DirectCast(WebRequest.Create(szURL1), HttpWebRequest)
    httpPutRequest.Credentials = New NetworkCredential(szUsername, szPassword, domain)
    httpPutRequest.PreAuthenticate = True
    httpPutRequest.Method = "PUT"
    httpPutRequest.Headers.Add("Overwrite", "T")
    httpPutRequest.ContentLength = szContent.Length

    'Optional, but allows for larger files.
    httpPutRequest.SendChunked = True

    Dim requestStream = httpPutRequest.GetRequestStream()

    'Write the string to the destination as a text file.
    requestStream.Write(System.Text.Encoding.UTF8.GetBytes(DirectCast(szContent, String)), 0, szContent.Length)

    'Close the request stream.
    requestStream.Close()

    'Retrieve the response.
    Dim httpPutResponse As HttpWebResponse = DirectCast(httpPutRequest.GetResponse(), HttpWebResponse)
    Debug.WriteLine("PUT Response #1: {0}", httpPutResponse.StatusDescription)
End Sub

This produces: PUT Response #1: OK and not PUT Response #1: CREATED

I took the code from: http://blogs.iis.net/robert_mcmurray/archive/2010/02/09/sending-webdav-requests-in-net.aspx and translated it to VB, but I do not think that translation is the problem.

Any ideas?

EDIT: I checked original C# code and the outcome is the same. What could be the problem?

Upvotes: 1

Views: 4588

Answers (2)

mimetnet
mimetnet

Reputation: 657

Having just dealt with this in another language I would suggest disabling SendChunked. The two aren't supposed to be mixed and SharePoint 2010 doesn't seem to like uploading unknown sizes.

Upvotes: 2

Michal B.
Michal B.

Reputation: 5719

I have no idea why the above does not work. I wrote this simple code and it works:

var client = new WebClient();
client.Credentials = new NetworkCredential(username, password, domain);
client.UploadData("http://host.domain.com/p/projects/4/Proposal/cze.txt", "PUT", Encoding.UTF8.GetBytes(DateTime.Now.ToString()));

If someone can answer the original question I will upvote it and accept it as an answer.

Upvotes: 0

Related Questions