Thursday 17 January 2008

The simplest way to upload documents and files to MOSS with the ASP.NET System.Net.WebClient class

It is odd that the Sharepoint 2007 Imaging Web Service has an Upload() Web Method, but the List Web service (lists.asmx) does not. To overcome this limitation, there is a very simple way to upload a file to your Sharepoint server using System.Net.WebClient (I have seen some methods using 30+ lines of code and HttpRequest, so it's best to use this method!):

//e.g. destinationFullURI = "http://dev-moss/sites/home/PropertySharePoint/DocumentLibrary/0/tmp167.tmp"
//sourceFullPath = "c:\\temp\myfiletoupload.tmp"

WebClient webClient = new WebClient();
webClient.Credentials = _credential;
webClient.UploadFile(destinationFullURI, "PUT", sourceFullPath);

Make sure you include the "PUT" method so it doesn't attempt an HTTP post (which is the default behaviour).

1 comment:

Oxanyo said...

It causes error. Conflit.. Thats only for MOSS or it can be done with WSS also?