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
Uploading a file to document library
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.