Home Contact

The Frog Pond of Technology

Ripples of Knowledge for SharePoint and Other .Net Technologies

News

 Subscribe to this blog


Upcoming speaker at:

About Me

Name:
Brian T. Jackett
Location:
Columbus, OH
Company:
Sogeti USA

Find me on...

Speaker Rate

Twitter












Archives

Post Categories

Syndication:

Programmatically Uploading Files To A SharePoint 2007 Document Library

    Uploading files to a SharePoint document library through the web UI is a fairly simple process, but did you know it’s also possible to programmatically upload a file (or string) to a document library?  Over the past two weeks I’ve had the latter scenario come up with friends and coworkers in several separate instances so I thought I’d post some simple code snippets on how to accomplish this.  I can’t take all credit as I borrowed the base code for uploading from a project my coworker Kelly Jones and I are on.  Below are two code snippets for first uploading a byte array representation of a string and second for uploading a file.

Uploading a string (converted to file) to a document library

string textToOutput = "Some text I want to put out to a file";
string uploadPath = "<URL for your document library + filename>";
// ex. http://server/myDocLib/SomeFile.csv
 
ASCIIEncoding encoder = new ASCIIEncoding();
byte[] bytesToOutput = encoder.GetBytes(textToOutput.ToString());
 
using (WebClient client = new WebClient())
{
    client.Credentials = System.Net.CredentialCache.DefaultCredentials;
    client.UploadData(uploadPath, "PUT", bytesToOutput);
}

Uploading a file to document library

string localFilename = "<path to file you wish to upload>";
// ex. c:\temp\myFileToUpload.doc
 
FileStream myStream = new FileStream(localFilename, FileMode.Open, FileAccess.Read);
BinaryReader myReader = new BinaryReader(myStream);
byte[] bytesToOutput = reader.ReadBytes((int)myStream.Length);
myReader.Close();
myStream.Close();
 
using (WebClient client = new WebClient())
{
    client.Credentials = System.Net.CredentialCache.DefaultCredentials;
    client.UploadData(uploadPath, "PUT", bytesToOutput);
}

 

Conclusion

    Programmatically uploading a file to a SharePoint document library is actually a fairly simple process as seen by the short code snippets above.  The basic premise is that you are performing an HttpRequest Put operation with the byte array representation of either a file or a string.  I’ve used this on a number of occasions at one of my current clients and it’s been very helpful.  Feel free to leave feedback if you found this helpful, have any questions, or suggestions for improvement.

 

    -Frog Out


Feedback

# re: Programmatically Uploading Files To A SharePoint 2007 Document Library

Thanks Alot........... 4/15/2010 1:30 AM | kusuma

# re: Programmatically Uploading Files To A SharePoint 2007 Document Library

I've try to use your code but I've got an error:
Unable to upload file (WebException) :
- Message : The remote server returned an error: (414) REQUEST URI TOO LONG.
- Response : System.Net.HttpWebResponse
- Status : ProtocolError
- Status Code : RequestUriTooLong
- Status Description : REQUEST URI TOO LONG
Can U Help Me? 5/3/2010 6:01 AM | Marco Guidone

# re: Programmatically Uploading Files To A SharePoint 2007 Document Library

Marco,

How long is the upload path that you are attempting to upload to? It sounds like the HttpRequest may be failing because your upload path is too long. If not that then the size of data (byte array) might be too large and being rejected by SharePoint. The default file size limit is 50 MB, but that can be configured higher if needed as long as you are ok with the associated performance hit. See the below link for additional details on 414 errors.

http://www.checkupdown.com/status/E414.html 5/3/2010 8:21 AM | Brian Jackett

Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: