PostRank Topblogs 2009 - #20 in Sharepoint

Windows Live Alerts web tracker
Chat with me if I'm online!
search blog
most popular
MCP MCTS MCT MVP

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 :-)

 

 
Posted on 10-Jun-10 by Tobias Zimmergren
21 Comments  |  Trackback Url  |  Link to this post | Bookmark this post with:        
Tags: 2010, Client Object Model, How To, SP2010, Tips
 

Links to this post (Trackbacks/Pingbacks)

Comments

Friday, 11 Jun 2010 11:30 by Donald
thanks!

Sunday, 13 Jun 2010 04:48 by Roger Bertilsson
tack tobbe. perfekt exempel att börja med för oss här på kontoret för våra nya appar!

Saturday, 19 Jun 2010 11:17 by Henrik
thx brotha! great example, using it now to build a upload-app for my desktop windows 7 machines. perfect!

Sunday, 20 Jun 2010 10:03 by Silent Coder
pretty useful shit man. thanks bro. see u in teched

Monday, 21 Jun 2010 07:41 by Ninja Awesome
Hayiiiaa! The SharePoint ninja awesome says thank you. *dissapears with smoke granades*

Wednesday, 23 Jun 2010 08:27 by Todd
u are amazing. thanks for just pushing out one after one good articles!

Monday, 30 Aug 2010 04:20 by SC Vinod
Hi Tobias, Thnks for the article. I need to know if it possible to upload a document from 1 document library to another document library using this code.

Name:
URL:
Email:
Comments: