SP 2010: Uploading files using the Client OM in SharePoint 2010

Author: Tobias Zimmergren
http://www.zimmergren.net | http://www.tozit.com | @zimmergren

Introduction

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:

  1. Type in the URL to your site where you want to upload your file
  2. Choose one of the available Document Libraries
  3. Click Upload a Document and you’ll get a browse-dialog to choose the file

Example:

1) Enter a servername and click Fetch Libraries
2) Select the Document library you want to upload your file to
image

3) Browse for a local file on your filesystem
image

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

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 = new OpenFileDialog();
    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:
http://www.zimmergren.net/archive/2009/11/30/sp-2010-getting-started-with-the-client-object-model-in-sharepoint-2010.aspx

Summary, References & Download

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.

References

  1. Getting Started with the Client Object Model
  2. FileCreationInformation

Download project

You can download this sample project here.

Enjoy this piece of awesomeness :-)

 

Follow me

Tobias Zimmergren

Founder / Consultant / Advisor at TOZIT AB
Tobias Zimmergren delivers high-quality articles about business and technology around the Microsoft scene.

Tobias focuses on advisory and consultancy for the Office 365 and SharePoint offerings from Microsoft.
Follow me
  • Orbsugita

    your download link on this article is broken. Where can I find the tool that is show in this article?

    • http://www.zimmergren.net/ Tobias Zimmergren

      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

  • Paul Hui

    hello Tobias,

    do you know how the file gets really uploaded ? chunks ? … ?

    what about uploading a big big file ?

    Thx you in advance

  • hideki katekawa

    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,

  • hideki katekawa

    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,

    • http://www.zimmergren.net/ Tobias Zimmergren

      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.

  • http://usajobstoday.us/ IT job listing

    I uncovered it informative and interesting. Excited for much more updates.

  • http://australiajobstoday.com/ IT jobs perth

    Thanks a lot for enjoying this beauty article with me. I am appreciating it very much! Looking forward to another great article. Good luck to the author! all the best!

  • http://highprbacklinks.co.in/ High PR Backlinks

    I was interesting, but it has become clear is beneficial. To update a lot of excitement.

  • http://www.einarmigerbandit24.com/ slotmaschine kostenlos spielen

    this is one amazing article! Thank you for posting this.

  • Micky

    Could this be adapted to upload multiple documents, with the same metadata?

    • http://www.zimmergren.net/ Tobias Zimmergren

      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.

  • Syed Asad Ullah Taimoor

    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

    • http://www.zimmergren.net/ Tobias Zimmergren

      Syed,

      Please refer questions outside of the scope of this article to http://www.mssharepointforums.com and I’m sure there’s plenty of people available to reply, myself included :-)

      Regards,
      Tobias.