bitUploading a file using the SharePoint client object model is easy, once you have loaded a reference to the SharePoint list you want to upload the file to. One strange thing though, via the Files collection of the List RootFolder, you can directly upload the file to any subfolder you like. It goes like this:
// FileInfo in System.IO namespace
var fileCreationInformation = new FileCreationInformation();
byte bytefile = System.IO.File.ReadAllBytes(“c:\\test\Test2.txt”);
fileCreationInformation.Content = bytefile;
fileCreationInformation.Overwrite = true;
fileCreationInformation.Url = “http://astro/MyLibrary/MyFolder/Test2.txt”;
// CurrentList is a client OM List object
The great thing about it is that you can use it to upload files in batches. But, that’s not the end of the story. If you do this, you must realize that the SharePoint client OM will use Base64 Encoding. According to WikiPedia (http://en.wikipedia.org/wiki/Base64), the final size of Base64-encoded binary data is equal to 1.37 times the original data size + 814 bytes (for headers). By the way, this is not our experience at all. In our testing, we’ve consistently found the message size to be considerably lower than the equivalent file size on the file system.
Things become more interesting… Check http://support.microsoft.com/kb/2529243 , it describes how to uploading of files larger than 3 MB fails (we did notice that), returning a WebException containing a response property that gives a (400) Bad Request error. This is caused by default internal restrictions on file sizes and timeout settings. Further more, the article says that,unless your files are really huge (like 1 GB) you can catch and ignore it and will notice that the files are uploaded just the same. The last thing doesn’t happen for us, it just fails to upload files larger than 3 MB.
But wait… There seems to be a solution. Article http://blogs.msdn.com/b/sridhara/archive/2010/03/12/uploading-files-using-client-object-model-in-sharepoint-2010.aspx explains how to increase the allowed message batch size using the managed client object model. It does use the server object model to accomplish this though, so be aware of this fact if you’re building a client tool that uploads files using the client OM (and please make sure to target it at x64, otherwise SPWebService.ContentService will return null). See http://msdn.microsoft.com/en-us/library/ff599489.aspx for more info. According to our testing, the SharePoint limit applies to the original file system size, not to the size of the file after Base64 Encoding it. Both articles fail to mention that you need to perform an IISRESET before changes take effect, but after doing so things will be alright.
If you’re interested in seeing this in action, check out the Migration Dragon for SharePoint 2010 at http://gallery.technet.microsoft.com/The-Migration-Dragon-for-628acae0 . It uses the SharePoint client object model to upload file and folder structures from the file system to SharePoint 2010 in batches using techniques discussed here.
Pingback: Another SharePoint 2010 Adventure :( | Promethix by Chris Law