With the increasing popularity of SharePoint 2010, and especially after the introduction of Office365, the market for client applications for SharePoint is increasing. In response, Microsoft has released the SharePoint Client Object Model, which is a wrapper library for the SharePoint web services.
In this post I will demonstrate how you can use the Client Object Model (dare we use the abbreviation COM?) to upload and download files to and from SharePoint. This may prove a central operation if you are migrating an existing SharePoint site to Office365. Other scenarios include uploading an existing file store to document libraries in a local SharePoint farm, or creating plug-in's to existing applications that need to process your documents.
The client context
The central class for the client code is the ClientContext. An instance of this class defines the connection to SharePoint, and any request, whether for getting or setting data, passes through the ClientContext instance. The general procedure is as folows:
- Define the requests you need to make using objects from the Client Object Model.
- Use the client context instance to execute the requests and load the data from SharePoint.
- Perform any processing of the data on the client side.
- (Optional) Use the client context instance to re-submit the data into SharePoint.
To download documents you need to know the URL to the document. This is stored in the document list item as item["FileRef"], or you may just happen to know what it is. No matter how we get it, we need the server-relative URL to the document. Once we have that, we can download the document with just a few lines of client code:
string fileName = fileRef.Substring(fileRef.LastIndexOf('/') + 1);
using (FileInformation fileInfo = File.OpenBinaryDirect(myContext, fileRef))
using (IO.FileStream writeStream = IO.File.Open(@"c:\MyFolder\" + fileName, IO.FileMode.Create))
Of course you can name the document whatever you want. The above is just a simple way of getting the original file name.
Uploading the document is not much more difficult than downloading it. In general, when we create things with the client object model, we need to make some kind of creation information object. For documents (and files in general) the class to use is the FileCreationInformation class.
createInfo.Content = System.IO.File.ReadAllBytes(@"c:\MyFolder\MyDocument.docx");
createInfo.Url = "MyDocument.docx";
File myFile = myLibrary.RootFolder.Files.Add(myCreateInfo);
This code will upload the document to the root folder of the document library. If you are not satisfied with this, you can find the desired folder by iterating through the folders and sub-folders using myLibrary.RootFolder.Folders.
Setting document metadata
Often we need to set some metadata on the documents. This can be achieved by using the list item connected to the file. Continuing from the code in the previous section:
myItem["myField"] = "myFieldvalue"]
//... set additional fields
You can add whatever fields you like. Just be sure to use the internal name of the field.
Summing it up
With the code above we can download files - documents, images, or anything - from SharePoint libraries. We can also upload files, and set their metadata.
The code works against any version of SharePoint, even SharePoint Online (as part of Office365). The last, however, requires the use of Live-ID when login in. In an upcoming post I will describe how to do this.