In this article I will guide your through the process of uploading a document to any chosen document library in SharePoint 2010 through the Client Object Model.
This application has a very simple usage scenario:
- Type in the URL to your site where you want to upload your file
- Choose one of the available Document Libraries
- Click Upload a Document and you’ll get a browse-dialog to choose the file
Example:
Enter a servername and click Fetch Libraries
Select the Document library you want to upload your file to

Browse for a local file on your filesystem[

Click the magic button (Upload) and you’ll see your document shoot straight into SharePoint from your client machine(s)[

How to utilize the Client Object Model in SharePoint 2010 to upload files
The most important thing to learn about when it comes to uploading files with the Client OM is to master the FileCreationInformation class that comes with the Client OM.
Take a look at this complete snippet to see how you can upload a file:
private void btnUploadDocument_Click(object sender, EventArgs e)
{
string library = listBox1.SelectedItem.ToString();
OpenFileDialog openDialog = newOpenFileDialog();
openDialog.Multiselect = false;
if (openDialog.ShowDialog() == DialogResult.OK)
{
SP.ClientContext ctx = new SP.ClientContext(tbSite.Text);
var web = ctx.Web;
var fciNewFileFromComputer = new SP.FileCreationInformation();
fciNewFileFromComputer.Content = File.ReadAllBytes(openDialog.FileName);
fciNewFileFromComputer.Url = Path.GetFileName(openDialog.FileName);
SP.List docs = web.Lists.GetByTitle(library);
SP.File uploadedFile = docs.RootFolder.Files.Add(fciNewFileFromComputer);
ctx.Load(uploadedFile);
ctx.ExecuteQuery();
// Tell your user that the file is uploaded. Awesomeness has been done!
MessageBox.Show(openDialog.FileName + " uploaded to " + library);
}
}
Things to note here is that I’m currently not changing the authentication for this application. That means it’ll be using your Windows/Domain Credentials.
Learn more about the authentication options here:
https://zimmergren.net/sp-2010-getting-started-with-the-client-object-model-in-sharepoint-2010/
Summary
With this sample application you can easily upload any file to any of your SharePoint 2010 document libraries by first entering the URL to the site, then selecting your library and finally browsing for a file and click OK.
Easily enough, you can change or extend this project the way you want it to work if you’ve got specific requirements to upload files using the Client OM.
Comments are closed
Archived comments
your download link on this article is broken. Where can I find the tool that is show in this article?
Hi Orbsugita,
When I migrated the blog, the download section wasn't migrated with the content. Feel free to use the sample code in the article and you'll have the same functionality as in the Windows App displayed above.
Cheers,
Tobias
hello Tobias,
do you know how the file gets really uploaded ? chunks ? ... ?
what about uploading a big big file ?
Thx you in advance
Hi Paul,
There's a great article on MSDN that I just googled up that tells you a bit about the file upload scenario: http://blogs.msdn.com/b/sri...
Regards,
Tobias.
Hello,
I'm developing an application for windows phone 7. One of its features is to be able to upload a file (documents, images, music) to a SharePoint 2010 site.
I would like to know if anyone has an idea on how to upload these files to SharePoint using a Windows Phone 7 mobile.
I mean, programmatically upload these files. Do I need a WebService or just the SharePoint site API? Do I need to add a webservice reference to my project?
Thanks in advance!
Best Regards,
Hi again,
I downloaded the project, but I don`t have the password. How can I unzip the file?
Thanks!
Sorry for the inconvenience
Best Regards,
Hi,
As you can see in the previous comments, the download link is broken after the blog migration.
You should be able to create a new project yourself and just copy/paste the code.
Regards,
Tobias.
Could this be adapted to upload multiple documents, with the same metadata?
Hi Micky,
Sure - it's just plain C# so whatever you want to do, you can do.
You should be able to alter it so that it'll upload more than one file through modifying the way you pick the files and then iterating those files and add them through the object model.
I don't have any examples at hand right now, so you'll have to live with a google search :-)
Cheers,
Tobias.
Hi I need to upload an image in list anonymously using the sandbox solution is there any way to do.
I have 3 fields (Name[string] , Phone[int] , Picture[Attachment])
i can insert Name and Phone anonymously but how do i upload the attatchment.
Please help
Syed,
Please refer questions outside of the scope of this article to www.mssharepointforums.com and I'm sure there's plenty of people available to reply, myself included :-)
Regards,
Tobias.
Hi Tobias,
Great Article. but i want to download a file form library to user local machine.
Any suggestion or link to achive.
Hi Navazee,
If you're simply trying to get a user to download a file, they can simply right-click a file and save a file, or use the "Send to -> Download a copy" function that are built into SharePoint by default.
If you're trying to download a file using code, there's multiple options - and a google search reveals them all:
- http://stackoverflow.com/qu...
- http://stackoverflow.com/qu...
- http://www.codeproject.com/...
etc.
Hope this helps.
Tobias.